Skip to content

Instantly share code, notes, and snippets.

@jolovin-mmi
Created April 12, 2017 20:31
Show Gist options
  • Save jolovin-mmi/2353aa87f0acd768491a609b1b08de83 to your computer and use it in GitHub Desktop.
Save jolovin-mmi/2353aa87f0acd768491a609b1b08de83 to your computer and use it in GitHub Desktop.
class Program
{
public static void Main(string[] args)
{
var docStore = new DocumentStore
{
Url = "http://apphost02.mountville.dmz:9000",
DefaultDatabase = "MMI"
};
docStore.Conventions.ReplicationInformerFactory = (url, jsonRequestFactory, requestTimeMetricGetter) => new CustomReplicationInformer(docStore.Conventions, jsonRequestFactory, requestTimeMetricGetter);
docStore.Initialize();
using (var session = docStore.OpenSession())
{
var testDoc = new TestDoc { Note = "Hello, World!" };
session.Store(testDoc);
session.SaveChanges();
}
docStore.Dispose();
}
}
public class CustomReplicationInformer : IDocumentStoreReplicationInformer
{
private const string ReadFromDb = "http://navraveneu.mountville.local:9000";
private const string WriteToDb = "http://apphost02.mountville.dmz:9000";
private readonly ReplicationInformer _informer;
public CustomReplicationInformer(DocumentConvention conventions, HttpJsonRequestFactory jsonRequestFactory, Func<string, IRequestTimeMetric> requestTimeMetricGetter)
{
_informer = new ReplicationInformer(conventions, jsonRequestFactory, requestTimeMetricGetter);
}
public event EventHandler<FailoverStatusChangedEventArgs> FailoverStatusChanged
{
add { _informer.FailoverStatusChanged += value; }
remove { _informer.FailoverStatusChanged -= value; }
}
public int DelayTimeInMiliSec
{
get { return _informer.DelayTimeInMiliSec; }
set { _informer.DelayTimeInMiliSec = value; }
}
public List<OperationMetadata> ReplicationDestinations
{
get { return _informer.ReplicationDestinations; }
}
public List<OperationMetadata> ReplicationDestinationsUrls
{
get { return _informer.ReplicationDestinationsUrls; }
}
public FailureCounters FailureCounters
{
get { return _informer.FailureCounters; }
}
public int GetReadStripingBase(bool increment)
{
return _informer.GetReadStripingBase(increment);
}
public ReplicationDestination[] FailoverServers
{
get { return _informer.FailoverServers; }
set { _informer.FailoverServers = value; }
}
public Task UpdateReplicationInformationIfNeededAsync(AsyncServerClient serverClient)
{
return _informer.UpdateReplicationInformationIfNeededAsync(serverClient);
}
public void ClearReplicationInformationLocalCache(ServerClient client)
{
_informer.ClearReplicationInformationLocalCache(client);
}
public void RefreshReplicationInformation(AsyncServerClient serverClient)
{
_informer.RefreshReplicationInformation(serverClient);
}
public void RefreshReplicationInformation(ServerClient serverClient)
{
_informer.RefreshReplicationInformation(serverClient);
}
public void Dispose()
{
_informer.Dispose();
}
public Task<T> ExecuteWithReplicationAsync<T>(HttpMethod method, string primaryUrl, OperationCredentials primaryCredentials,
int currentRequest, int currentReadStripingBase, Func<OperationMetadata, IRequestTimeMetric, Task<T>> operation, CancellationToken token = new CancellationToken())
{
primaryUrl = method.Method == "GET" ? ReadFromDb : WriteToDb; //This is the only thing I care about
return _informer.ExecuteWithReplicationAsync(method, primaryUrl, primaryCredentials, currentRequest, currentReadStripingBase, operation, token);
}
}
public class TestDoc
{
public string Id { get; set; }
public string Note { get; set; }
public DateTime TimeStamp { get; set; }
public TestDoc()
{
Id = "TestDocs/";
TimeStamp = DateTime.Now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment