Quorum is a convenience class for working wit quorums.
Ejemplo n.º 1
0
        /// <summary>
        /// Create a table in this database.
        /// </summary>
        /// <param name="quorum">The first shard of the table is placed inside the <see cref="Scalien.Quorum"/>.</param>
        /// <param name="name">The name of the table.</param>
        /// <returns>The <see cref="Scalien.Table"/> object corresponding to the created table.</returns>
        /// <exception cref="SDBPException"/>
        /// <seealso cref="CreateTable(string)"/>
        public virtual Table CreateTable(string name, Quorum quorum)
        {
            int status = scaliendb_client.SDBP_CreateTable(client.cptr, databaseID, quorum.QuorumID, name);

            client.CheckResultStatus(status);
            return(GetTable(name));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Start  transaction inside the quorum on majorKey
        /// </summary>
        /// <seealso cref="CommitTransaction()"/>
        /// <seealso cref="RollbackTransaction()"/>
        public void StartTransaction(Quorum quorum, byte[] majorKey)
        {
            int status;

            unsafe
            {
                fixed(byte *pKey = majorKey)
                {
                    IntPtr ipKey = new IntPtr(pKey);

                    status = scaliendb_client.SDBP_StartTransactionCStr(cptr, quorum.QuorumID, ipKey, majorKey.Length);
                }
            }

            result = new Result(scaliendb_client.SDBP_GetResult(cptr));
            if (status < 0)
            {
                if (status == Status.SDBP_FAILED)
                {
                    TransactionException exception = new LockTimeoutException(status);
                    UpdateException(exception, result);
                    throw exception;
                }
                CheckStatus(result, status);
            }
            result.Close();
        }
Ejemplo n.º 3
0
        public static bool RandomShardServerHTTPAction(Client client, Quorum quorum, string action)
        {
            var configState  = Utils.GetFullConfigState(client);
            var shardServers = configState.shardServers;
            var configQuorum = configState.quorums.First(cq => cq.quorumID == (long)quorum.QuorumID);
            var random       = new Random();

            // select shard server
            var victimNodeID = configQuorum.activeNodes[random.Next(configQuorum.activeNodes.Count)];

            foreach (var shardServer in shardServers)
            {
                if (shardServer.nodeID == victimNodeID)
                {
                    var httpURI  = ConfigStateHelpers.GetShardServerURL(shardServer);
                    var response = Utils.HTTP.GET(Utils.HTTP.BuildUri(httpURI, action));
                    if (response == null)
                    {
                        return(false);
                    }
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a table in this database, with the first shard placed in the first available quorum.
        /// </summary>
        /// <param name="name">The name of the table.</param>
        /// <returns>The <see cref="Scalien.Table"/> object corresponding to the created table.</returns>
        /// <exception cref="SDBPException"/>
        /// <seealso cref="Scalien.Table"/>
        /// <seealso cref="CreateTable(string, Quorum)"/>
        public virtual Table CreateTable(string name)
        {
            List <Quorum> quorums = client.GetQuorums();

            if (quorums.Count == 0)
            {
                throw new SDBPException(Status.SDBP_BADSCHEMA, "No quorums found");
            }
            Quorum quorum = quorums[0];

            return(CreateTable(name, quorum));
        }
Ejemplo n.º 5
0
        public static bool PrimaryShardServerHTTPAction(Client client, Quorum quorum, string action)
        {
            var configState = Utils.GetFullConfigState(client);
            var shardServers = configState.shardServers;
            var configQuorum = configState.quorums.First(cq => cq.quorumID == (long)quorum.QuorumID);
            if (configQuorum.hasPrimary == false)
                return false;

            var shardServer = configState.shardServers.First(s => s.nodeID == configQuorum.primaryID);
            var httpURI = ConfigStateHelpers.GetShardServerURL(shardServer);
            var response = Utils.HTTP.GET(Utils.HTTP.BuildUri(httpURI, action));
            if (response == null)
                return false;

            return true;
        }
Ejemplo n.º 6
0
        public static bool RandomShardServerHTTPAction(Client client, Quorum quorum, string action)
        {
            var configState = Utils.GetFullConfigState(client);
            var shardServers = configState.shardServers;
            var configQuorum = configState.quorums.First(cq => cq.quorumID == (long)quorum.QuorumID);
            var random = new Random();

            // select shard server
            var victimNodeID = configQuorum.activeNodes[random.Next(configQuorum.activeNodes.Count)];
            foreach (var shardServer in shardServers)
            {
                if (shardServer.nodeID == victimNodeID)
                {
                    var httpURI = ConfigStateHelpers.GetShardServerURL(shardServer);
                    var response = Utils.HTTP.GET(Utils.HTTP.BuildUri(httpURI, action));
                    if (response == null)
                        return false;
                    return true;
                }
            }
            return false;
        }
Ejemplo n.º 7
0
        public static bool PrimaryShardServerHTTPAction(Client client, Quorum quorum, string action)
        {
            var configState  = Utils.GetFullConfigState(client);
            var shardServers = configState.shardServers;
            var configQuorum = configState.quorums.First(cq => cq.quorumID == (long)quorum.QuorumID);

            if (configQuorum.hasPrimary == false)
            {
                return(false);
            }

            var shardServer = configState.shardServers.First(s => s.nodeID == configQuorum.primaryID);
            var httpURI     = ConfigStateHelpers.GetShardServerURL(shardServer);
            var response    = Utils.HTTP.GET(Utils.HTTP.BuildUri(httpURI, action));

            if (response == null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 8
0
        public static void ThreadFunc()
        {
            //Client.SetTrace(true);
            //Client.SetLogFile("d:/out.txt");

            //string[] controllers = { "127.0.0.1:7080" };
            string[] controllers = { "192.168.137.110:7080", "192.168.137.111:7080", "192.168.137.112:7080" };
            Client   client      = new Client(controllers);

            Quorum   quorum  = client.GetQuorum("test");
            Database db      = client.GetDatabase("test");
            Table    test    = db.GetTable("test");
            Table    indices = db.GetTable("indices");
            Sequence IDs     = indices.GetSequence("IDs");

            //System.Console.WriteLine("Thread.CurrentThread.ManagedThreadId = {0}", Thread.CurrentThread.ManagedThreadId);

            System.Random random = new System.Random(Thread.CurrentThread.ManagedThreadId);

            var value = "";

            while (value.Length < 10 * 1000)
            {
                value += "" + random.Next();
            }

            //using (client.Transaction(quorum, "foo" + random.Next(100)))
            //{
            //    System.Console.WriteLine("Transaction started.");
            //    for (var j = 0; j < 10*1000; j++)
            //            table.Set("" + j, value);

            //    client.CommitTransaction();
            //    System.Console.WriteLine("Transaction finished.");
            //}
            //return;

            ulong ID;

            //for (var i = 0; i < 1000; i++)
            while (true)
            {
                if (random.Next(3) == 0)
                {
                    try
                    {
                        System.Console.WriteLine("Batch started.");
                        for (var j = 0; j < 10; j++)
                        {
                            if (random.Next(2) == 0)
                            {
                                test.Set("" + random.Next(1000 * 1000), value);
                            }
                            else
                            {
                                test.Delete("" + random.Next(1000 * 1000));
                            }
                        }
                        //for (var j = 0; j < 10 * 1000; j++)
                        //    ID = IDs.GetNext;
                        client.Submit();
                        System.Console.WriteLine("Batch finished.");
                    }
                    catch (SDBPException)
                    {
                        System.Console.WriteLine("Batch failed.");
                    }
                    continue;
                }

                //if (random.Next(2) == 0)
                //{
                //    try
                //    {
                //        System.Console.WriteLine("List started.");
                //        var count = 0;
                //        foreach (KeyValuePair<string, string> kv in test.GetKeyValueIterator(new StringRangeParams().Prefix("1")))
                //        {
                //            count++;
                //            //System.Console.WriteLine(kv.Key + " => " + kv.Value);
                //            if (count == 10)
                //                break;
                //        }
                //        System.Console.WriteLine("List finished.");
                //    }
                //    catch (SDBPException)
                //    {
                //        System.Console.WriteLine("List failed.");
                //        // why does this happen
                //    }
                //    continue;
                //}

                while (true)
                {
                    try
                    {
                        using (client.Transaction(quorum, "foo" + random.Next(100)))
                        {
                            System.Console.WriteLine("Transaction started.");
                            for (var j = 0; j < 10; j++)
                            {
                                if (random.Next(2) == 0)
                                {
                                    test.Set("" + random.Next(1000 * 1000), value);
                                }
                                else
                                {
                                    test.Delete("" + random.Next(1000 * 1000));
                                }
                            }
                            //for (var j = 0; j < 1000; j++)
                            //    ID = IDs.GetNext;
                            if (random.Next(2) == 0)
                            {
                                client.CommitTransaction();
                            }
                            else
                            {
                                client.RollbackTransaction();
                            }
                            System.Console.WriteLine("Transaction finished.");
                            break;
                        }
                    }
                    catch (SDBPException)
                    {
                        //System.Console.WriteLine("Exception.");
                        System.Threading.Thread.Sleep(1);
                    }
                }
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Start a real, server-side transaction.
 /// </summary>
 /// <remarks>
 /// Return a <see cref="Scalien.Committer"/> object that will automatically call <see cref="Commit()"/> when it goes out of scope.
 /// </remarks>
 /// <returns>A <see cref="Scalien.Committer"/> object that will automatically call <see cref="Commit()"/> when it goes out of scope.</returns>
 /// <seealso cref="Submit()"/>
 /// <seealso cref="Cancel()"/>
 public Rollbacker Transaction(Quorum quorum, byte[] majorKey)
 {
     return(new Rollbacker(this, quorum, majorKey));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Start a real, server-side transaction.
 /// </summary>
 /// <remarks>
 /// Return a <see cref="Scalien.Rollbacker"/> object that will automatically call <see cref="RollbackTransaction()"/> when it goes out of scope.
 /// </remarks>
 /// <returns>A <see cref="Scalien.Rollbacker"/> object that will automatically call <see cref="RollbackTransaction()"/> when it goes out of scope.</returns>
 /// <seealso cref="StartTransaction()"/>
 /// <seealso cref="CommitTransaction()"/>
 /// <seealso cref="RollbackTransaction()"/>
 public Rollbacker Transaction(Quorum quorum, string majorKey)
 {
     return(Transaction(quorum, StringToByteArray(majorKey)));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Start  transaction inside the quorum on majorKey
 /// </summary>
 /// <seealso cref="CommitTransaction()"/>
 /// <seealso cref="RollbackTransaction()"/>
 public void StartTransaction(Quorum quorum, string majorKey)
 {
     StartTransaction(quorum, StringToByteArray(majorKey));
 }
Ejemplo n.º 12
0
        internal Rollbacker(Client client, Quorum quorum, byte[] majorKey)
        {
            this.client = client;

            client.StartTransaction(quorum, majorKey);
        }
Ejemplo n.º 13
0
        internal Rollbacker(Client client, Quorum quorum, byte[] majorKey)
        {
            this.client = client;

            client.StartTransaction(quorum, majorKey);
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Create a table in this database.
 /// </summary>
 /// <param name="quorum">The first shard of the table is placed inside the <see cref="Scalien.Quorum"/>.</param>
 /// <param name="name">The name of the table.</param>
 /// <returns>The <see cref="Scalien.Table"/> object corresponding to the created table.</returns>
 /// <exception cref="SDBPException"/>
 /// <seealso cref="CreateTable(string)"/>
 public virtual Table CreateTable(string name, Quorum quorum)
 {
     int status = scaliendb_client.SDBP_CreateTable(client.cptr, databaseID, quorum.QuorumID, name);
     client.CheckResultStatus(status);
     return GetTable(name);
 }