Leader election for snapshots.
Leader will watch for truncation and user snapshot requests
342062
private void leaderElection() {
        loggingLog.info("Starting leader election for snapshot truncation daemon");
        try {
            while (true) {
                Stat stat = m_zk.exists(VoltZK.snapshot_truncation_master, new Watcher() {
                    @Override
                    public void process(WatchedEvent event) {
                        switch(event.getType()) {
                        case NodeDeleted:
                            loggingLog.info("Detected the snapshot truncation leader's ephemeral node deletion");
                            m_es.execute(new Runnable() {
                                @Override
                                public void run() {
                                    leaderElection();
                                }
                            });
                            break;
                            default:
                                break;
                        }
                    }
                });
                if (stat == null) {
                    try {
                        m_zk.create(VoltZK.snapshot_truncation_master, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                        m_isAutoSnapshotLeader = true;
                        if (m_lastKnownSchedule != null) {
                            makeActivePrivate(m_lastKnownSchedule);
                        }
                        electedTruncationLeader();
                        return;
                    } catch (NodeExistsException e) {
                    }
                } else {
                    loggingLog.info("Leader election concluded, a leader already exists");
                    break;
                }
            }
        } catch (Exception e) {
            VoltDB.crashLocalVoltDB("Exception in snapshot daemon electing master via ZK", true, e);
        }
    }
335948
def ensure_sufficient_node_managers(snapshots)
      currently_sufficient = true
      snapshots.each do |node, snapshot|
        node_managers = snapshot.node_managers
        if node_managers.size < @required_node_managers
          logger.error("Not enough Node Managers in snapshot for node #{node}. " +
            "Required: #{@required_node_managers}, " +
            "Available: #{node_managers.size} #{node_managers}")
          currently_sufficient = false
        end
      end

      if currently_sufficient && !@sufficient_node_managers
        logger.info("Required Node Managers are visible: #{@required_node_managers}")
      end

      @sufficient_node_managers = currently_sufficient
      @sufficient_node_managers
    end
1080500
def filter(self, table, snapshots, filter_string):
        """"""
        query = filter_string.lower()
        return [snapshot for snapshot in snapshots
                if query in snapshot.name.lower()]
416362
private void processSnapshotTruncationRequestCreated(
            final WatchedEvent event) {
        loggingLog.info("Snapshot truncation leader received snapshot truncation request");
        // Get the truncation request ID which is the truncation request node path.
        final String truncReqId;
        try {
            List<String> children = m_zk.getChildren(event.getPath(), false);
            if (children.isEmpty()) {
                loggingLog.error("Unable to retrieve truncation snapshot request id from ZK, log can't be truncated");
                return;
            }
            truncReqId = ZKUtil.joinZKPath(event.getPath(), Collections.max(children));

        } catch (Exception e) {
            loggingLog.error("Unable to retrieve truncation snapshot request ID from ZK, log can't be truncated");
            return;
        }
        final long now = System.currentTimeMillis();
        final String nonce = Long.toString(now);
        //Allow nodes to check and see if the nonce incoming for a snapshot is
        //for a truncation snapshot. In that case they will mark the completion node
        //to be for a truncation snapshot. SnapshotCompletionMonitor notices the mark.
        // TRAIL [TruncSnap:7] write current ts to request zk node data
        try {
            ByteBuffer payload = ByteBuffer.allocate(8);
            payload.putLong(0, now);
            m_zk.setData(VoltZK.request_truncation_snapshot, payload.array(), -1);
        } catch (Exception e) {
            //Cause a cascading failure?
            VoltDB.crashLocalVoltDB("Setting data on the truncation snapshot request in ZK should never fail", true, e);
        }
        // for the snapshot save invocations
        JSONObject jsObj = new JSONObject();
        try {
            assert truncReqId != null;
            String sData = "";
            JSONObject jsData = new JSONObject();
            jsData.put("truncReqId", truncReqId);
            sData = jsData.toString();
            jsObj.put(SnapshotUtil.JSON_PATH, VoltDB.instance().getCommandLogSnapshotPath() );
            jsObj.put(SnapshotUtil.JSON_NONCE, nonce);
            jsObj.put(SnapshotUtil.JSON_PATH_TYPE, SnapshotPathType.SNAP_CL);
            jsObj.put("perPartitionTxnIds", retrievePerPartitionTransactionIds());
            jsObj.put("data", sData);
        } catch (JSONException e) {
            /*
             * Should never happen, so fail fast
             */
            VoltDB.crashLocalVoltDB("", true, e);
        }

        // for the snapshot save invocations
        long handle = m_nextCallbackHandle++;

        // for the snapshot save invocation
        m_procedureCallbacks.put(handle, new ProcedureCallback() {

            @Override
            public void clientCallback(ClientResponse clientResponse)
                    throws Exception {
                m_lastInitiationTs = null;
                if (clientResponse.getStatus() != ClientResponse.SUCCESS){
                    loggingLog.warn(
                            "Attempt to initiate a truncation snapshot was not successful: " +
                            clientResponse.getStatusString());
                    loggingLog.warn("Retrying log truncation snapshot in 5 minutes");
                    /*
                     * TRAIL [TruncSnap:8] (callback) on failed response try again in a few minute
                     */
                    m_es.schedule(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                processTruncationRequestEvent(event);
                            } catch (Exception e) {
                                VoltDB.crashLocalVoltDB("Error processing snapshot truncation request event", true, e);
                            }
                        }
                    }, 5, TimeUnit.MINUTES);
                    return;
                }

                final VoltTable results[] = clientResponse.getResults();
                final VoltTable result = results[0];
                boolean success = true;

                final String err = SnapshotUtil.didSnapshotRequestFailWithErr(results);
                if (err != null) {
                    if (err.trim().equalsIgnoreCase("SNAPSHOT IN PROGRESS")) {
                        loggingLog.info("Snapshot is in progress");
                    } else {
                        loggingLog.warn("Snapshot failed with failure response: " + err);
                    }
                    success = false;
                }

                //assert(result.getColumnName(1).equals("TABLE"));
                if (success) {
                    while (result.advanceRow()) {
                        if (!result.getString("RESULT").equals("SUCCESS")) {
                            success = false;
                            loggingLog.warn("Snapshot save feasibility test failed for host "
                                    + result.getLong("HOST_ID") + " table " + result.getString("TABLE") +
                                    " with error message " + result.getString("ERR_MSG"));
                        }
                    }
                }

                if (success) {
                    loggingLog.info("Snapshot initiation for log truncation was successful");

                    JSONObject obj = new JSONObject(clientResponse.getAppStatusString());
                    final long snapshotTxnId = Long.valueOf(obj.getLong("txnId"));
                    try {
                        boolean found = false;
                        ZKUtil.VoidCallback lastCallback = null;

                        for (String child: m_zk.getChildren(event.getPath(),false)) {
                            String requestId = ZKUtil.joinZKPath(event.getPath(), child);
                            found = found || requestId.equals(truncReqId);

                            lastCallback = new ZKUtil.VoidCallback();
                            m_zk.delete(requestId, -1, lastCallback, null);
                        }

                        if (lastCallback != null) {
                            try {
                                lastCallback.get();
                            } catch (KeeperException.NoNodeException ignoreIt) {
                            }
                        }
                        if (!found) {
                            VoltDB.crashLocalVoltDB(
                                    "Could not match truncations snapshot request id while atepting its removal", true, null);
                        }
                    } catch (Exception e) {
                        VoltDB.crashLocalVoltDB(
                                "Unexpected error deleting truncation snapshot request", true, e);
                    }

                    try {
                        TruncationSnapshotAttempt snapshotAttempt =
                            m_truncationSnapshotAttempts.get(snapshotTxnId);
                        if (snapshotAttempt == null) {
                            snapshotAttempt = new TruncationSnapshotAttempt();
                            m_truncationSnapshotAttempts.put(snapshotTxnId, snapshotAttempt);
                            snapshotAttempt.pathType = SnapshotPathType.SNAP_CL.toString();
                        }
                        snapshotAttempt.nonce = nonce;
                        snapshotAttempt.path = VoltDB.instance().getCommandLogSnapshotPath();
                    } finally {
                        // TRAIL [TruncSnap:9] (callback) restart the whole request check cycle
                        try {
                            truncationRequestExistenceCheck();
                        } catch (Exception e) {
                            VoltDB.crashLocalVoltDB(
                                    "Unexpected error checking for existence of truncation snapshot request"
                                    , true, e);
                        }
                    }
                } else {
                    loggingLog.info("Retrying log truncation snapshot in 60 seconds");
                    /*
                     * TRAIL [TruncSnap:10] (callback) on table reported failure try again in a few minutes
                     */
                    m_es.schedule(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                processTruncationRequestEvent(event);
                            } catch (Exception e) {
                                VoltDB.crashLocalVoltDB("Exception processing truncation request event", true, e);
                            }
                        }
                    }, 1, TimeUnit.MINUTES);
                }
            }

        });
        try {
            loggingLog.info("Initiating @SnapshotSave for log truncation");
            initiateSnapshotSave(handle, new Object[]{jsObj.toString(4)}, false);
        } catch (JSONException e) {
            /*
             * Should never happen, so fail fast
             */
            VoltDB.crashLocalVoltDB("", true, e);
        }
        return;
    }
1023701
def failover_strategy_candidate(snapshots)
      # only include nodes that this master Node Manager can see
      filtered_snapshots = snapshots.select do |node, snapshot|
        snapshot.viewable_by?(manager_id)
      end

      logger.info('Attempting to find candidate from snapshots:')
      logger.info("\n" + filtered_snapshots.values.join("\n"))
      @failover_strategy.find_candidate(filtered_snapshots)
    end
386220
def get_snapshot_policies(self, view=None):
    
    return self._get("snapshots/policies", ApiSnapshotPolicy, True,
        params=view and dict(view=view) or None, api_version=6)
701569
async def wait_for_election_success(cls):
        """"""
        if cls.leader is None:
            cls.leader_future = asyncio.Future(loop=cls.loop)
            await cls.leader_future
553436
def poll(self):
        

        pubsub = self.conn.pubsub()
        pubsub.subscribe(self.key)

        # Pubsub messages generator
        gen = pubsub.listen()

        last_heartbeat = time.time()
        self.heartbeat()

        last_cleanup = time.time()
        self.cleanup()

        try:
            while True:
                # Since Redis' listen method blocks, we use select to inspect the
                # underlying socket to see if there is activity.
                fileno = pubsub.connection._sock.fileno()
                timeout = max(0, POLL_INTERVAL - (time.time() - last_heartbeat))
                r, w, x = self._select([fileno], [], [], timeout)
                if fileno in r:
                    next(gen)
                    self.update()

                last_heartbeat = time.time()
                self.heartbeat()

                now = time.time()
                if now - last_cleanup > CLEANUP_INTERVAL:
                    last_cleanup = now
                    self.cleanup()
        finally:
            pubsub.close()
847972
public function takeSnapshot()
    {
        $this->enabledTypeBefore = null;

        if ($this->typeDescriptors->contains($this->typeName)) {
            foreach ($this->typeDescriptors->listByTypeName($this->typeName) as $typeDescriptor) {
                if ($typeDescriptor->isEnabled()) {
                    $this->enabledTypeBefore = $typeDescriptor;
                }
            }
        }

        $this->snapshotTaken = true;
    }
1248666
def swap_leader(self, new_leader):
        
        # Replica set cannot be changed
        assert(new_leader in self._replicas)
        curr_leader = self.leader
        idx = self._replicas.index(new_leader)
        self._replicas[0], self._replicas[idx] = \
            self._replicas[idx], self._replicas[0]
        return curr_leader
910758
private void ensureLastAdminIsNotRemoved(DbSession dbSession, GroupDto group, UserDto user) {
    int remainingAdmins = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingGroupMember(dbSession,
      group.getOrganizationUuid(), OrganizationPermission.ADMINISTER.getKey(), group.getId(), user.getId());
    checkRequest(remainingAdmins > 0, "The last administrator user cannot be removed");
  }
260036
def update_snapshot(self, snapshot, display_name=None,
            display_description=None):
        
        return snapshot.update(display_name=display_name,
                display_description=display_description)
815389
def on_receive_request_vote_response(self, data):
        

        if data.get('vote_granted'):
            self.vote_count += 1

            if self.state.is_majority(self.vote_count):
                self.state.to_leader()
327409
protected boolean updateTermAndLeader(long term, MemberId leader) {
    // If the request indicates a term that is greater than the current term or no leader has been
    // set for the current term, update leader and term.
    if (term > raft.getTerm() || (term == raft.getTerm() && raft.getLeader() == null && leader != null)) {
      raft.setTerm(term);
      raft.setLeader(leader);

      // Reset the current cluster configuration to the last committed configuration when a leader change occurs.
      raft.getCluster().reset();
      return true;
    }
    return false;
  }
634549
protected synchronized void completeSnapshot(Snapshot snapshot) {
    checkNotNull(snapshot, "snapshot cannot be null");

    Map.Entry<Long, Snapshot> lastEntry = snapshots.lastEntry();
    if (lastEntry == null) {
      snapshots.put(snapshot.index(), snapshot);
    } else if (lastEntry.getValue().index() < snapshot.index()) {
      snapshots.put(snapshot.index(), snapshot);
      Snapshot lastSnapshot = lastEntry.getValue();
      lastSnapshot.close();
      lastSnapshot.delete();
    } else if (storage.isRetainStaleSnapshots()) {
      snapshots.put(snapshot.index(), snapshot);
    } else {
      snapshot.close();
      snapshot.delete();
    }
  }
377772
def stop_periodic_snapshots(self):
        
        if self._periodic_thread and self._periodic_thread.isAlive():
            self._periodic_thread.stop = True
            self._periodic_thread.join()
            self._periodic_thread = None
803439
private void setMigratePartitionLeaderStatus(MigratePartitionLeaderMessage message) {

        //The host with old partition leader is down.
        if (message.isStatusReset()) {
            m_migratePartitionLeaderStatus = MigratePartitionLeaderStatus.NONE;
            return;
        }

        if (m_migratePartitionLeaderStatus == MigratePartitionLeaderStatus.NONE) {
            //txn draining notification from the old leader arrives before this site is promoted
            m_migratePartitionLeaderStatus = MigratePartitionLeaderStatus.TXN_DRAINED;
        } else if (m_migratePartitionLeaderStatus == MigratePartitionLeaderStatus.TXN_RESTART) {
            //if the new leader has been promoted, stop restarting txns.
            m_migratePartitionLeaderStatus = MigratePartitionLeaderStatus.NONE;
        }

        tmLog.info("MigratePartitionLeader new leader " +
                CoreUtils.hsIdToString(m_hsId) + " is notified by previous leader " +
                CoreUtils.hsIdToString(message.getPriorLeaderHSID()) + ". status:" + m_migratePartitionLeaderStatus);
    }