Ejemplo n.º 1
0
        public ReaderWriter(INode creator,
                            IMessageHub messageHub,
                            IEnumerable <IConfiguration> configMap,
                            IMessageSerializer serializer)
        {
            var configurations = configMap.ToDictionary(c => c.Key, c => c);

            this.serializer = serializer;
            op = new CurrentOperation
            {
                Phase            = new ObservableAtomicValue <OperationPhase>(OperationPhase.Idle),
                Type             = new ObservableAtomicValue <OperationType>(OperationType.Idle),
                Accepted         = new ObservableConcurrentDictionary <int, INode>(Enumerable.Empty <KeyValuePair <int, INode> >()),
                ConfigurationMap = new ObservableConcurrentDictionary <IConfigurationIndex, IConfiguration>(configurations)
            };
            gc = new GarbageCollectionOperation {
                Phase = new ObservableAtomicValue <OperationPhase>(OperationPhase.Idle)
            };
            this.creator    = creator;
            this.configMap  = new ObservableConcurrentDictionary <IConfigurationIndex, IConfiguration>(configurations);
            this.messageHub = messageHub;
            value           = new ObjectValue {
                Value = 0
            };
            tag            = new Tag(creator);
            localPhase     = new PhaseNumber();
            operationQueue = new BlockingCollection <OperationRequest>(new ConcurrentQueue <OperationRequest>());
            eventHandlers  = new EventHandlerList();
            status         = new ObservableAtomicValue <NodeStatus>(NodeStatus.Idle);
            world          = new ObservableConcurrentDictionary <int, INode>();
            phaseVector    = new ObservableConcurrentDictionary <INode, IPhaseNumber>();

            preJoinAck  = new ObservableCondition(() => status.Get() == NodeStatus.Active, new[] { status });
            preOutSend  = new ObservableCondition(() => status.Get() == NodeStatus.Active, new[] { status });
            preQueryFix = new ObservableCondition(QueryFixCondition,
                                                  new IChangeNotifiable[] { status, op.Type, op.Phase, op.Accepted, op.ConfigurationMap });
            prePropagationFix = new ObservableCondition(PropagationFixCondition,
                                                        new IChangeNotifiable[] { status, op.Type, op.Phase, op.Accepted, op.ConfigurationMap });
            preReadAck = new ObservableCondition(() => status.Get() == NodeStatus.Active &&
                                                 op.Type.Get() == OperationType.Read &&
                                                 op.Phase.Get() == OperationPhase.Done,
                                                 new IChangeNotifiable[] { status, op.Type, op.Phase });
            preWriteAck = new ObservableCondition(() => status.Get() == NodeStatus.Active &&
                                                  op.Type.Get() == OperationType.Write &&
                                                  op.Phase.Get() == OperationPhase.Done,
                                                  new IChangeNotifiable[] { status, op.Type, op.Phase });
            new Thread(ProcessReadWriteRequests).Start();
            new Thread(OutJoinAck).Start();
            new Thread(OutSend).Start();
            new Thread(IntQueryFix).Start();
            new Thread(IntPropagationFix).Start();
            new Thread(OutReadAck).Start();
            new Thread(OutWriteAck).Start();
            var listener = messageHub.Subscribe(creator);

            listener.Where(m => m.Body.MessageType.ToMessageType() == MessageTypes.JoinRw)
            .Subscribe(new MessageStreamListener(OnJoinReceived));
            listener.Where(m => m.Body.MessageType.ToMessageType() == MessageTypes.Gossip)
            .Subscribe(new MessageStreamListener(OnGossipReceived));
        }
Ejemplo n.º 2
0
 private void MergeWorld(IObservableConcurrentDictionary <int, INode> localWorld, IEnumerable <INode> senderWorld)
 {
     foreach (var node in senderWorld)
     {
         localWorld[node.Id] = node;
     }
 }
Ejemplo n.º 3
0
        private bool Usable(IObservableConcurrentDictionary <IConfigurationIndex, IConfiguration> configurationMap)
        {
            var gc     = configurationMap.Where(entry => entry.Value.State == ConfigurationState.GCed);
            var active = configurationMap.Where(entry => entry.Value.State == ConfigurationState.Active);

            return(MonitonicallyIncreasing(gc.Select(c => c.Key), active.Select(c => c.Key)));
        }
Ejemplo n.º 4
0
 private void ExtendConfigurationMap(IObservableConcurrentDictionary <IConfigurationIndex, IConfiguration> localConfiguration,
                                     IEnumerable <KeyValuePair <IConfigurationIndex, IConfiguration> > senderConfig)
 {
     foreach (var configuration in senderConfig)
     {
         if (!localConfiguration.ContainsKey(configuration.Key))
         {
             localConfiguration[configuration.Key] = configuration.Value;
         }
     }
 }
Ejemplo n.º 5
0
 private void UpdateConfigurationMap(IObservableConcurrentDictionary <IConfigurationIndex, IConfiguration> localConfiguration,
                                     IEnumerable <KeyValuePair <IConfigurationIndex, IConfiguration> > senderConfig)
 {
     foreach (var configuration in senderConfig)
     {
         if (configuration.Value.State == ConfigurationState.GCed || localConfiguration[configuration.Key].State == ConfigurationState.GCed)
         {
             localConfiguration[configuration.Key] = new GarbageCollectedConfiguration(configuration.Key);
         }
         else if (configuration.Value.State == ConfigurationState.Active)
         {
             localConfiguration[configuration.Key] = configuration.Value;
         }
     }
 }