// -----------------------------------------------------------------------
//
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
//
// -----------------------------------------------------------------------
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Rachis.Transport;
using Raven.Abstractions.Connection;
using Raven.Abstractions.Data;
using Raven.Abstractions.Replication;
using Raven.Client.Connection;
using Raven.Client.Document;
using Raven.Database.Raft.Util;
using Raven.Imports.Newtonsoft.Json;
using Raven.Json.Linq;
using Raven.Tests.Common;
using Xunit;
using Xunit.Extensions;
namespace Raven.Tests.Raft
{
public class Building
{
public int Floors { get; set; }
}
public class ClusterDatabaseIssue : RaftTestBase
{
[Fact]
public void DatabaseShouldBeCreatedOnAllNodes()
{
var clusterStores = CreateRaftCluster(2);
var store0 = clusterStores[0];
store0.DatabaseCommands.GlobalAdmin.CreateDatabase(new DatabaseDocument
{
Id = "Buildings",
Settings =
{
{"Raven/DataDir", "~/Databases/Northwind"}
}
});
var key = Constants.Database.Prefix + "Buildings";
clusterStores.ForEach(store => WaitForDocument(store.DatabaseCommands.ForSystemDatabase(), key));
clusterStores.ForEach(x=>x.DefaultDatabase= "Buildings");
foreach (var store in clusterStores)
{
using (var session = store.OpenSession())
{
session.Store(new ReplicationConfig()
{
DocumentConflictResolution = StraightforwardConflictResolution.ResolveToRemote
}, Constants.RavenReplicationConfig);
session.SaveChanges();
}
}
foreach (var store in clusterStores)
{
store.Conventions.FailoverBehavior = FailoverBehavior.ReadFromAllWriteToLeaderWithFailovers;
store.FailoverServers = new FailoverServers();
store.FailoverServers.AddForDatabase("Buildings", clusterStores.Where(x=>x.Url!= store.Url).Select(x => new ReplicationDestination()
{
Database = "Buildings",
Url = x.Url,
TransitiveReplicationBehavior = TransitiveReplicationOptions.Replicate
}).ToArray());
}
using (ForceNonClusterRequests(clusterStores))
{
for (var i = 0; i < clusterStores.Count; i++)
{
clusterStores[i ].DatabaseCommands.Put("Buildings/1", null, RavenJObject.FromObject(new Building() { Floors = 1}), new RavenJObject());
for (var j = 0; j < i-1; j++)
{
clusterStores[i].DatabaseCommands.Put("Buildings/1", null, RavenJObject.FromObject(new Building { Floors = 1}), new RavenJObject());
}
}
}
for (int i = 0; i < 20; i++)
{
using (var session = store0.OpenSession(new OpenSessionOptions()
{
ForceReadFromMaster = true
}))
{
var bucket = session.Load("Buildings/1");
bucket.Floors++;
session.SaveChanges();
}
}
foreach (var store in clusterStores)
{
store.Dispose();
}
}
}
}