Exemple #1
0
        /// <summary>
        /// Close closes the store. If wait is true, waits for a graceful shutdown.
        /// Once closed, a Store may not be re-opened.
        /// </summary>
        /// <param name="wait"></param>
        public void Close(bool wait)
        {
            lock (closedMu)
            {
                if (closed)
                {
                    return;
                }
                done.Writer.TryComplete();
                wg.SignalAndWait();

                // XXX CLOSE OTHER CONNECTIONS
                dbConn.Close();
                dbConn = null;
                db     = null;

                if (raft != null)
                {
                    raft.Stop();
                    raft = null;
                }

                raftLog = null;

                closed = true;
            }
        }
Exemple #2
0
        /// <summary>
        /// Open opens the store. If enableSingle is set, and there are no existing peers,
        /// then this node becomes the first node, and therefore leader, of the cluster.
        /// </summary>
        /// <param name="enableSingle"></param>
        public void Open(bool enableSingle)
        {
            lock (closedMu)
            {
                if (closed)
                {
                    throw new StoreInvalidStateException();
                }
                logger.LogInformation($"opening store with node ID {ID}");
                logger.LogInformation($"ensuring directory at {Path} exists");
                var di = Directory.CreateDirectory(Path);
                //di.GetAccessControl().SetAccessRule(new FileSystemAccessRule()) //TODO: Set perms to 0755

                // Create underlying database.
                createDatabase();


                // Get utility connection to database.
                dbConn = db.Connect();

                conns.Add(defaultConnID, new StoreConnection(dbConn, this, defaultConnID, TimeSpan.Zero, TimeSpan.Zero));

                // Is this a brand new node?
                var raftLogDbPath = System.IO.Path.Combine(Path, "raft.db");
                var newNode       = !File.Exists(raftLogDbPath);
                raftLog = new Raft.SQLiteLog(raftLogDbPath, new NodeId(ID), Logging.LoggerFactory);

                /*
                 *              // Create Raft-compatible network layer.
                 *              raftTn = raft.NewNetworkTransport(NewTransport(ln),
                 *      connectionPoolCount, connectionTimeout, nil);
                 *
                 *              // Get the Raft configuration for this store.
                 *              var config = raftConfig();
                 *
                 *              config.LocalID = raft.ServerID(s.raftID);
                 *
                 *              config.Logger = log.New(os.Stderr, "[raft] ", log.LstdFlags);
                 *
                 *              // Create the snapshot store. This allows Raft to truncate the log.
                 *              snapshots, err:= raft.NewFileSnapshotStore(s.raftDir, retainSnapshotCount, os.Stderr)
                 *
                 *  if err != nil {
                 *                  return fmt.Errorf("file snapshot store: %s", err)
                 *
                 *  }
                 *
                 *              // Create the log store and stable store.
                 *              s.boltStore, err = raftboltdb.NewBoltStore(filepath.Join(s.raftDir, "raft.db"))
                 *
                 *  if err != nil {
                 *                  return fmt.Errorf("new bolt store: %s", err)
                 *
                 *  }
                 *              s.raftStable = s.boltStore
                 *
                 *  s.raftLog, err = raft.NewLogCache(raftLogCacheSize, s.boltStore)
                 *
                 *  if err != nil {
                 *                  return fmt.Errorf("new cached store: %s", err)
                 *
                 *  }
                 */
                raftPeers = new PeersProvider();
                // Instantiate the Raft system.
                raft = new Node(this, raftLog, raftSettings, raftPeers, Logging.LoggerFactory);
                // ra, err:= raft.NewRaft(config, s, s.raftLog, s.raftStable, snapshots, s.raftTn)

                //     if err != nil {
                //     return fmt.Errorf("new raft: %s", err)

                //     }

                /*
                 *
                 *          if enableSingle && newNode {
                 *              s.logger.Printf("bootstrap needed")
                 *
                 *
                 *  configuration:= raft.Configuration{
                 *              Servers: []raft.Server{
                 *                      {
                 *                      ID: config.LocalID,
                 *              Address: raftTn.LocalAddr(),
                 *          },
                 *      },
                 *  }
                 *              ra.BootstrapCluster(configuration)
                 *
                 *
                 * }
                 *          else
                 *          {
                 *              logger.Printf("no bootstrap needed")
                 *
                 *
                 * }
                 *
                 *          raft = ra
                 */
                // Start connection monitoring
                checkConnections();
            }
        }