예제 #1
0
        /// <summary>
        /// Directly enumerates the contents of BPlusTree from disk in read-only mode.
        /// </summary>
        /// <param name="options"> The options normally used to create the <see cref="BPlusTree{TKey, TValue}"/> instance </param>
        /// <returns> Yields the Key/Value pairs found in the file </returns>
        public static IEnumerable <KeyValuePair <TKey, TValue> > EnumerateFile(BPlusTreeOptions <TKey, TValue> options)
        {
            options            = options.Clone();
            options.CreateFile = CreatePolicy.Never;
            options.ReadOnly   = true;

            using (INodeStorage store = options.CreateStorage())
            {
                bool           isnew;
                Node           root;
                IStorageHandle hroot = store.OpenRoot(out isnew);
                if (isnew)
                {
                    yield break;
                }

                NodeSerializer nodeReader = new NodeSerializer(options, new NodeHandleSerializer(store));
                if (isnew || !store.TryGetNode(hroot, out root, nodeReader))
                {
                    throw new InvalidDataException();
                }

                Stack <KeyValuePair <Node, int> > todo = new Stack <KeyValuePair <Node, int> >();
                todo.Push(new KeyValuePair <Node, int>(root, 0));

                while (todo.Count > 0)
                {
                    KeyValuePair <Node, int> cur = todo.Pop();
                    if (cur.Value == cur.Key.Count)
                    {
                        continue;
                    }

                    todo.Push(new KeyValuePair <Node, int>(cur.Key, cur.Value + 1));

                    Node child;
                    if (!store.TryGetNode(cur.Key[cur.Value].ChildNode.StoreHandle, out child, nodeReader))
                    {
                        throw new InvalidDataException();
                    }

                    if (child.IsLeaf)
                    {
                        for (int ix = 0; ix < child.Count; ix++)
                        {
                            var set        = child[ix].ToKeyValuePair();
                            var enumerator = set.Value.GetEnumerator();
                            while (enumerator.MoveNext())
                            {
                                yield return(new KeyValuePair <TKey, TValue>(set.Key, enumerator.Current.Key));
                            }
                        }
                    }
                    else
                    {
                        todo.Push(new KeyValuePair <Node, int>(child, 0));
                    }
                }
            }
        }
예제 #2
0
        public RaftService(
            ILoggerFactory logger,
            IOptions <ClusterOptions> clusterOptions,
            IOptions <NodeOptions> nodeOptions,
            IClusterConnectionPool clusterConnectionPool,
            INodeStorage <State> nodeStorage,
            IStateMachine <State> stateMachine,
            NodeStateService nodeStateService,
            ClusterClient clusterClient
            ) : base(logger.CreateLogger <RaftService <State> >(), clusterOptions.Value, nodeOptions.Value, stateMachine, nodeStateService)
        {
            _nodeStorage   = nodeStorage;
            _loggerFactory = logger;
            //Bootstrap the node
            _snapshotService = new Snapshotter <State>(logger.CreateLogger <Snapshotter <State> >(), nodeStorage, stateMachine, nodeStateService);

            _bootstrapService      = new Bootstrapper <State>(logger.CreateLogger <Bootstrapper <State> >(), clusterOptions.Value, nodeOptions.Value, nodeStorage, StateMachine, NodeStateService);
            _commitService         = new CommitService <State>(logger.CreateLogger <CommitService <State> >(), clusterOptions.Value, nodeOptions.Value, nodeStorage, StateMachine, NodeStateService);
            _discovery             = new Discovery(logger.CreateLogger <Discovery>());
            _clusterClient         = clusterClient;
            _clusterConnectionPool = clusterConnectionPool;
            NodeStateService.Id    = _nodeStorage.Id;

            _electionTimeoutTimer = new Timer(ElectionTimeoutEventHandler);
            _heartbeatTimer       = new Timer(HeartbeatTimeoutEventHandler);

            if (!ClusterOptions.TestMode)
            {
                _bootstrapTask = Task.Run(async() =>
                {
                    //Wait for the rest of the node to bootup
                    Logger.LogInformation("Starting bootstrap...");
                    Thread.Sleep(3000);
                    nodeStateService.Url            = await _bootstrapService.GetMyUrl(ClusterOptions.GetClusterUrls(), TimeSpan.FromMilliseconds(ClusterOptions.LatencyToleranceMs));
                    NodeStateService.IsBootstrapped = true;
                    SetNodeRole(NodeState.Follower);
                });
            }
            else
            {
                Logger.LogInformation("Running in test mode...");
                SetNodeRole(NodeState.Leader);
                NodeStateService.IsBootstrapped = true;
                Handle(new ExecuteCommands()
                {
                    Commands = new List <BaseCommand>()
                    {
                        {
                            new UpsertNodeInformation()
                            {
                                Id               = NodeStateService.Id,
                                Name             = "",
                                TransportAddress = "https://localhost:5021",
                                IsContactable    = true
                            }
                        }
                    }
                }).GetAwaiter().GetResult();
            }
        }
예제 #3
0
파일: RadixTree.cs 프로젝트: lulzzz/Sperone
 public RadixTree(INodeStorage nodeStorage, IValueStorage <TValue> valueStorage, ISerializer <TKey> keySerializer, int maxPrefixLength)
 {
     _nodeStorage    = nodeStorage;
     _valueStorage   = valueStorage;
     _keySerializer  = keySerializer;
     MaxPrefixLength = maxPrefixLength;
 }
 public StorageCache(INodeStorage store, int sizeLimit)
 {
     _store     = store;
     _sizeLimit = sizeLimit;
     _lock      = new SimpleReadWriteLocking();
     _cache     = new Dictionary <IStorageHandle, StorageInfo>();
     _ordered   = new Queue <IStorageHandle>();
 }
예제 #5
0
 public Snapshotter(ILogger <Snapshotter <State> > logger,
                    INodeStorage <State> nodeStorage,
                    IStateMachine <State> stateMachine,
                    NodeStateService nodeStateService)
 {
     _logger           = logger;
     _stateMachine     = stateMachine;
     _nodeStorage      = nodeStorage;
     _nodeStateService = nodeStateService;
 }
예제 #6
0
            public StorageCache(INodeStorage store, int sizeLimit)
            {
                _asyncThreshold   = 50;
                _writeBehindFunc  = Flush;
                _asyncWriteBehind = null;

                _store              = store;
                _cache              = new LurchTable <IStorageHandle, object>(LurchTableOrder.Access, sizeLimit, 1000000, sizeLimit >> 4, 1000, EqualityComparer <IStorageHandle> .Default);
                _dirty              = new LurchTable <IStorageHandle, object>(LurchTableOrder.Modified, sizeLimit, 1000000, sizeLimit >> 4, 1000, EqualityComparer <IStorageHandle> .Default);
                _dirty.ItemRemoved += OnItemRemoved;
            }
예제 #7
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _storage = null;
            }

            _disposed = true;
        }
예제 #8
0
 public NodeController(IClusterRequestHandler handler,
                       ILogger <NodeController <State> > logger,
                       NodeStateService nodeStateService,
                       IStateMachine <State> stateMachine,
                       INodeStorage <State> nodeStorage,
                       IClusterConnectionPool clusterConnectionPool,
                       IShardRepository shardRepository)
 {
     _handler               = handler;
     Logger                 = logger;
     _nodeStateService      = nodeStateService;
     _stateMachine          = stateMachine;
     _nodeStorage           = nodeStorage;
     _clusterConnectionPool = clusterConnectionPool;
     _shardRepository       = shardRepository;
 }
예제 #9
0
        public AppendEntriesRPC_Test()
        {
            var sp = TestUtility.GetFullNodeProvider();

            Node        = sp.GetService <IRaftService>();
            NodeStorage = sp.GetService <INodeStorage <TestState> >();
            NodeStorage.AddLogs(new System.Collections.Generic.List <LogEntry>()
            {
                new LogEntry()
                {
                    Commands = new List <BaseCommand>(),
                    Index    = 2,
                    Term     = 5
                }
            });
            NodeStorage.SetCurrentTerm(5);
        }
예제 #10
0
        public Bootstrapper(ILogger <Bootstrapper <State> > logger,
                            ClusterOptions clusterOptions,
                            NodeOptions nodeOptions,
                            INodeStorage <State> nodeStorage,
                            IStateMachine <State> stateMachine,
                            NodeStateService nodeStateService)
        {
            _nodeStorage = nodeStorage;
            Logger       = logger;

            //Load the last snapshot
            if (_nodeStorage.LastSnapshot != null)
            {
                Logger.LogInformation("Detected snapshot, loading snapshot into state.");
                stateMachine.ApplySnapshotToStateMachine(_nodeStorage.LastSnapshot);
                nodeStateService.CommitIndex = _nodeStorage.LastSnapshotIncludedIndex;
                _nodeStorage.SetCurrentTerm(_nodeStorage.LastSnapshotIncludedTerm);
                nodeStateService.Id = _nodeStorage.Id;
            }
        }
예제 #11
0
 public BPlusTree(INodeStorage <TKey> nodeStorage, IValueStorage <TValue> valueStorage)
 {
     _nodeStorage  = nodeStorage;
     _valueStorage = valueStorage;
 }
예제 #12
0
 public CommitService(ILogger <CommitService <State> > logger, ClusterOptions clusterOptions, NodeOptions nodeOptions, INodeStorage <State> nodeStorage, IStateMachine <State> stateMachine, NodeStateService nodeStateService) : base(logger, clusterOptions, nodeOptions, stateMachine, nodeStateService)
 {
     _nodeStorage = nodeStorage;
     _logger      = logger;
 }
예제 #13
0
 public BTreeIndex(INodeStorage <TNode, TKey> storage)
 {
     _storage = storage;
 }
 public GraphController(IGraph graph, INodeStorage nodeStorage)
 {
     this.graph = graph;
     this.nodeStorage = nodeStorage;
 }
예제 #15
0
파일: RadixTree.cs 프로젝트: lulzzz/Sperone
 public RadixTree(INodeStorage nodeStorage, IValueStorage <TValue> valueStorage, ISerializer <TKey> keySerializer)
     : this(nodeStorage, valueStorage, keySerializer, nodeStorage.MaxPrefixLength)
 {
 }
 public DependencyResolver(INodeStorage nodeStorage, IGraph graph)
 {
     this.nodeStorage = nodeStorage;
     this.graph = graph;
 }