Example #1
0
        private bool TryReadValue(DataStoreKeyDto object_key, string partition_id)
        {
            string    result;
            ReadReply reply;

            try
            {
                reply = client.Read(new ReadRequest {
                    ObjectKey = object_key
                });
                if (reply.ObjectExists)
                {
                    result = reply.Object.Val;
                    Console.WriteLine(">>> Read Result: " + result);
                    return(CompareClock(partition_id, reply.PartitionClock, object_key));
                }

                Console.WriteLine(">>> Object does not exist...");
                return(false);
            }
            catch
            {
                // server is crashed
                bool canRetryOperation = HandleCrashedServer(attached_server_id);
                if (canRetryOperation)
                {
                    Console.WriteLine(">>> Retrying <Read> after reattaching to new master");
                    TryReadValue(object_key, partition_id);
                }

                return(false);
            }
        }
Example #2
0
        // Change name to reflect boolean return
        private bool CompareClock(string partition_id, int reply_clock, DataStoreKeyDto object_key)
        {
            // Do Clock related staff
            int partition_highest_clock = PartitionMapping.partitionToClockMapping[partition_id];

            Console.WriteLine(">>> PartitionClock=" + partition_highest_clock + ", ReplyClock=" + reply_clock);
            if (reply_clock > partition_highest_clock)
            {
                Console.WriteLine(">>> ReplyClock > PartitionClock. Reply clock is acceptable. ");
                PartitionMapping.UpdatePartitionClock(partition_id, reply_clock);
                return(true);
            }
            else if (reply_clock < partition_highest_clock)
            {
                Console.WriteLine(">>> ReplyClock < PartitionClock. Waiting " + retry_time.ToString() + "And trying again...");
                wait(retry_time);
                return(TryReadValue(object_key, partition_id));
            }

            else if (reply_clock == partition_highest_clock)
            {
                Console.WriteLine(">>> PartitionClock == ReplyClock. Reply clock is acceptable. ");
            }
            return(true);
        }
Example #3
0
        private void write(string partition_id, string object_id, string value)
        {
            WriteReply reply;

            if (debug_console)
            {
                Console.WriteLine("Get Partition Master from Partition Named: " + partition_id);
            }
            string partition_master_server_id = PartitionMapping.GetPartitionMaster(partition_id);

            if (debug_console)
            {
                Console.WriteLine("Partition Master Server ID: " + partition_master_server_id);
            }
            reattachServer(partition_master_server_id);

            var object_key = new DataStoreKeyDto
            {
                PartitionId = partition_id,
                ObjectId    = object_id
            };

            var object_value = new DataStoreValueDto
            {
                Val = value
            };

            Console.WriteLine(">>> Write request...");

            try
            {
                reply = client.Write(new WriteRequest {
                    ObjectKey = object_key, Object = object_value
                });
                Console.WriteLine("Write result: " + reply);
            }
            catch
            {
                bool canRetryOperation = HandleCrashedServer(attached_server_id);
                if (canRetryOperation)
                {
                    Console.WriteLine(">>> Retrying <Write> after reattaching to new master");
                    write(partition_id, object_id, value);
                }

                return;
            }
        }
Example #4
0
        private void read(string partition_id, string object_id, string server_id)
        {
            string    result     = "N/A";
            bool      got_result = false;
            ReadReply reply;

            var object_key = new DataStoreKeyDto
            {
                PartitionId = partition_id,
                ObjectId    = object_id
            };

            if (debug_console)
            {
                Console.WriteLine("Reading from the server...");
            }

            Console.WriteLine(">>> Read request...");

            // if the client is attached to a server and that server contains the desired partition
            List <string> available_partitions_in_server = PartitionMapping.getPartitionsByServerID(attached_server_id);

            if (!string.IsNullOrEmpty(attached_server_id) && available_partitions_in_server.Contains(partition_id))
            {
                if (debug_console)
                {
                    Console.WriteLine("Reading from the Attached Server: " + attached_server_id);
                }

                // read value from attached server
                try
                {
                    reply = client.Read(new ReadRequest {
                        ObjectKey = object_key
                    });
                    if (reply.ObjectExists)
                    {
                        result     = reply.Object.Val;
                        got_result = true;
                    }
                    // server is crashed
                } catch
                {
                    handle_crashed_server(attached_server_id);
                    got_result = false;
                }
            }

            // if theres no result yet and there is a valid server_id parameter
            if ((!got_result) && (!server_id.Equals("-1")))
            {
                // check if the server hint even has the partition
                available_partitions_in_server = PartitionMapping.getPartitionsByServerID(server_id);
                if (available_partitions_in_server.Contains(partition_id))
                {
                    if (debug_console)
                    {
                        Console.WriteLine("Attach to new Server: " + server_id);
                    }
                    reattachServer(server_id);

                    // read value from alternative server
                    try
                    {
                        reply = client.Read(new ReadRequest {
                            ObjectKey = object_key
                        });
                        if (reply.ObjectExists)
                        {
                            result     = reply.Object.Val;
                            got_result = true;
                        }
                        // server is crashed
                    } catch
                    {
                        handle_crashed_server(attached_server_id);
                        got_result = false;
                    }
                }
            }

            // if theres no result yet, the client should find a server serving partition_id on its own
            // it will try to connect to every single node in that partition
            if (!got_result)
            {
                string[] partition_nodes = PartitionMapping.getPartitionAllNodes(partition_id);

                foreach (string node_id in partition_nodes)
                {
                    if (debug_console)
                    {
                        Console.WriteLine("Attach to new Server: " + node_id);
                    }
                    reattachServer(node_id);

                    // read value from one of the partition servers
                    try
                    {
                        reply = client.Read(new ReadRequest {
                            ObjectKey = object_key
                        });
                        if (reply.ObjectExists)
                        {
                            result     = reply.Object.Val;
                            got_result = true;
                        }
                        // server is crashed
                    } catch
                    {
                        handle_crashed_server(attached_server_id);
                        got_result = false;
                    }
                }
            }

            if (got_result == false)
            {
                result = "N/A";
            }
            Console.WriteLine("Read Result: " + result);
        }
 public void lockReplicas(Dictionary <string, ServerCommunicationService.ServerCommunicationServiceClient> replicas, DataStoreKeyDto key)
 {
     foreach (string replica_id in replicas.Keys)
     {
         try
         {
             replicas[replica_id].LockObject(new lockRequest
             {
                 PartitionId = key.PartitionId,
                 ObjectId    = key.ObjectId
             });
         }
         catch (Exception)
         {
             Console.WriteLine("Replica cannot be reached: " + replica_id);
             replicas.Remove(replica_id);
         }
     }
 }