예제 #1
0
        public override async Task ExecuteAsync(List <string> arguments)
        {
            if (arguments.Count != EXPECTED_ARGUMENTS)
            {
                Console.WriteLine("Expected " + EXPECTED_ARGUMENTS + " arguments but found " + arguments.Count + ".");
                return;
            }

            string partitionId = arguments.ElementAt(0);
            string objectId    = arguments.ElementAt(1);
            string serverId    = arguments.ElementAt(2);

            try
            {
                Console.WriteLine($"Read... {partitionId} {objectId} {serverId}");
                GStoreObject gstoreObject = await ReadController.Execute(ConnectionManager, partitionId, serverId, objectId);

                Console.WriteLine($"PartitionId: {gstoreObject.Identifier.PartitionId} | ObjectId: {gstoreObject.Identifier.ObjectId} | Value: {gstoreObject.Value}");
            }
            catch (ServerBindException e)
            {
                Console.WriteLine($"ERROR: {e.Message}");
            }
            catch (RpcException ex) when(ex.StatusCode == StatusCode.Internal)
            {
                Console.WriteLine($"Could not establish connection with server.");
            }
        }
예제 #2
0
        public static async Task ExecuteAsync(ConnectionManager connectionManager, GStoreObject gStoreObject, IDictionary <string, int> replicaLocks)
        {
            IDictionary <string, Task> writeTasks = new Dictionary <string, Task>();

            foreach (KeyValuePair <string, int> replicaLock in replicaLocks)
            {
                string replicaId = replicaLock.Key;
                int    lockId    = replicaLock.Value;
                writeTasks.Add(replicaId, ExecuteReplicaAsync(connectionManager, replicaId, gStoreObject, lockId));
            }

            // Await lock write requests
            foreach (KeyValuePair <string, Task> writeTaskPair in writeTasks)
            {
                string replicaId = writeTaskPair.Key;
                try
                {
                    await writeTaskPair.Value;
                }
                catch (Grpc.Core.RpcException e) when(e.StatusCode == Grpc.Core.StatusCode.Internal)
                {
                    connectionManager.DeclareDead(replicaId);
                }
            }
        }
예제 #3
0
        public async Task Write(GStoreObjectIdentifier gStoreObjectIdentifier, string newValue)
        {
            try
            {
                if (!connectionManager.IsMasterForPartition(gStoreObjectIdentifier.PartitionId))
                {
                    throw new Exception("Not master");
                }

                // Acquire lock on local object
                ReaderWriterLockEnhancedSlim objectLock = GetObjectLock(gStoreObjectIdentifier);
                int lockId = objectLock.EnterWriteLock();

                // Send lock requests to all remote objects
                IDictionary <string, int> replicaLocks = await LockController.ExecuteAsync(connectionManager, gStoreObjectIdentifier);

                // Once lock confirmations arrive, write to local object and unlock it
                GStoreObject gStoreObject = AddOrUpdate(gStoreObjectIdentifier, newValue);
                objectLock.ExitWriteLock(lockId);

                // Send write requests to all remote objects
                await WriteReplicaController.ExecuteAsync(connectionManager, gStoreObject, replicaLocks);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        public static async Task ExecuteAsync(ConnectionManager connectionManager, GStoreObject gStoreObject, int version)
        {
            GStoreObjectIdentifier gStoreObjectIdentifier = gStoreObject.Identifier;

            // Get all replicas associated to this Partition
            IImmutableSet <Server> servers = connectionManager.GetAliveServers(gStoreObjectIdentifier.PartitionId);

            IDictionary <string, Task> writeTasks = new Dictionary <string, Task>();

            foreach (Server server in servers)
            {
                if (server.Id != connectionManager.SelfServerId)
                {
                    writeTasks.Add(server.Id, ExecuteServerAsync(connectionManager, server.Stub, gStoreObject, version));
                }
            }

            foreach (KeyValuePair <string, Task> writeTaskPair in writeTasks)
            {
                string serverId = writeTaskPair.Key;
                try
                {
                    await writeTaskPair.Value;
                }
                catch (Grpc.Core.RpcException e) when(e.StatusCode == Grpc.Core.StatusCode.Internal)
                {
                    connectionManager.DeclareDead(serverId);
                }
            }
        }
        private static GStoreObjectReplica CreateObjectReplica(DataObjectReplica dataObjectReplica)
        {
            GStoreObjectIdentifier gStoreObjectIdentifier = new GStoreObjectIdentifier(dataObjectReplica.Object.ObjectIdentifier.PartitionId, dataObjectReplica.Object.ObjectIdentifier.ObjectId);
            GStoreObject           gStoreObject           = new GStoreObject(gStoreObjectIdentifier, dataObjectReplica.Object.Value);

            return(new GStoreObjectReplica(gStoreObject, dataObjectReplica.IsMasterReplica));
        }
예제 #6
0
        public void Write(GStoreObjectIdentifier gStoreObjectIdentifier, string newValue)
        {
            // Acquire lock on local object
            ReaderWriterLockSlim objectLock = GetObjectLock(gStoreObjectIdentifier);

            objectLock.EnterWriteLock();
            try
            {
                int version = GetAndIncrementObjectVersionNumber(gStoreObjectIdentifier);
                AddOrUpdateObjectVersionServerWriter(gStoreObjectIdentifier, connectionManager.SelfServerId);
                GStoreObject gStoreObject = AddOrUpdate(gStoreObjectIdentifier, newValue);

                _ = WriteReplicaController.ExecuteAsync(connectionManager, gStoreObject, version);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                objectLock.ExitWriteLock();
            }
        }
예제 #7
0
        private static async Task ExecuteReplicaAsync(ConnectionManager connectionManager, string replicaId, GStoreObject gStoreObject, int lockId)
        {
            WriteRequest writeRequest = new WriteRequest
            {
                LockId = lockId,
                Object = new ObjectDto
                {
                    ObjectIdentifier = new ObjectIdentifierDto
                    {
                        PartitionId = gStoreObject.Identifier.PartitionId,
                        ObjectId    = gStoreObject.Identifier.ObjectId
                    },
                    Value = gStoreObject.Value
                }
            };

            Server replica = connectionManager.GetAliveServer(replicaId);
            await replica.Stub.WriteAsync(writeRequest);
        }
        private static async Task ExecuteServerAsync(ConnectionManager connectionManager, MasterReplicaService.MasterReplicaServiceClient stub, GStoreObject gStoreObject, int version)
        {
            WriteRequest writeRequest = new WriteRequest
            {
                Object = new ObjectDto
                {
                    ObjectIdentifier = new ObjectIdentifierDto
                    {
                        PartitionId = gStoreObject.Identifier.PartitionId,
                        ObjectId    = gStoreObject.Identifier.ObjectId
                    },
                    Value = gStoreObject.Value
                },
                ServerId = connectionManager.SelfServerId,
                Version  = version
            };

            await stub.WriteAsync(writeRequest);
        }