Implementing Bridge Conflict Resolver
In bridge topology, there can be more than one active site cache and cache operations can be simultaneous. It is possible that the client connected with one active site cache adds some keys and the same key operation is performed by the other active node. In such a situation the bridge conflict resolver comes into play. It resolves the conflicts and decides which key to apply on the cache depending upon the defined rules.
If the user hasn't configured any conflict resolver rules, then NCache uses the default resolver rule. In the default rule, a timespan is added with each operation performed on the cache and it keeps the latest operation. A check is performed on the version of the bridge item. If it has a newer time stamp or if the version is the latest or the same it replaces that with the new entry and in case of old, it keeps the old entry.
IBridgeConflictResolver Interface
To utilize the interface, include the following namespace in your application:
Alachisoft.NCache.Runtime.Bridge.
Method | Description |
---|---|
Init |
This method performs tasks like allocating resources, acquiring connections etc. When cache is initialized and Conflict Resolvers are enabled, Init() method is called to notify the client that cache is initializing and you can initialize your conflict resolver via Init() . The parameters passed as an argument contains all the parameters (if any) that were specified using NCache Manager cache/cluster views. These parameters can be utilized in many ways. |
Dispose |
This method performs tasks like releasing the resources etc. When the cache is stopped or the task is completed, it calls the Dispose() method to notify the client that resources related to the task can be freed. |
Resolve |
This method contains the logic to resolve any conflict that may occur while replicating an operation from bridge for a key that already exists in the cache. Whenever any conflict occurs, the cache calls BridgeConflictResolution to decide which operation to apply on the cache depending upon rules defined in it. It takes two parameters/entries, both are of ProviderBridgeItem type. ProviderBridgeItem contains the following parameters: |
BridgeItemVersion
contains version of an item (LATEST, OLD, SAME).Key
contains key of an item.Value
contains value of an item.BridgeItemOpCodes
contains conflicted operation (Add
,Remove
,Update
,Clear
,RemoveGroup
,RegisterKeyNotification
).
Sample Implementation of IBridgeConflictResolver Interface
public class Resolver : IBridgeConflictResolver
{
private bool _enableLogging;
private TextWriter _textLogger;
private string streamLog = "bridgeResolverLog.txt"
public void Dispose()
{
if (_enableLogging)
{
_textLogger.Close();
}
}
public void Init(System.Collections.IDictionary parameters)
{
//Initialize textWriter
if(parameters.Contains("logging")){
_enableLogging = Convert.ToBoolean(parameters["logging"]);
}
if (_enableLogging){
_textLogger = new StreamWriter(streamLog);
}
}
public ConflictResolution Resolve(ProviderBridgeItem oldEntry, ProviderBridgeItem newEntry)
{
ConflictResolution conflictResolution = new ConflictResolution();
switch (oldEntry.BridgeItemVersion)
{
case BridgeItemVersion.OLD:
{
conflictResolution.ResolutionAction = ResolutionAction.ReplaceWithNewEntry;
}
break;
case BridgeItemVersion.LATEST:
{
conflictResolution.ResolutionAction = ResolutionAction.KeepOldEntry;
}
break;
case BridgeItemVersion.SAME:
{
if (oldEntry.OpCode == BridgeItemOpCodes.Remove){
conflictResolution.ResolutionAction = ResolutionAction.ReplaceWithNewEntry;
}
else{
conflictResolution.ResolutionAction = ResolutionAction.KeepOldEntry;
}
}
break;
}
if (_enableLogging){
//perform operations
}
return conflictResolution;
}
}