Search in sources :

Example 1 with Stat

use of org.apache.zookeeper_voltpatches.data.Stat in project voltdb by VoltDB.

the class InvocationDispatcher method takeShutdownSaveSnapshot.

private final ClientResponseImpl takeShutdownSaveSnapshot(final StoredProcedureInvocation task, final InvocationClientHandler handler, final Connection ccxn, final AuthUser user, OverrideCheck bypass) {
    // shutdown save snapshot is available for Pro edition only
    if (!MiscUtils.isPro()) {
        task.setParams();
        return dispatch(task, handler, ccxn, user, bypass, false);
    }
    Object p0 = task.getParams().getParam(0);
    final long zkTxnId;
    if (p0 instanceof Long) {
        zkTxnId = ((Long) p0).longValue();
    } else if (p0 instanceof String) {
        try {
            zkTxnId = Long.parseLong((String) p0);
        } catch (NumberFormatException e) {
            return gracefulFailureResponse("Incorrect argument type", task.clientHandle);
        }
    } else {
        return gracefulFailureResponse("Incorrect argument type", task.clientHandle);
    }
    VoltDBInterface voltdb = VoltDB.instance();
    if (!voltdb.isPreparingShuttingdown()) {
        log.warn("Ignoring shutdown save snapshot request as VoltDB is not shutting down");
        return unexpectedFailureResponse("Ignoring shutdown save snapshot request as VoltDB is not shutting down", task.clientHandle);
    }
    final ZooKeeper zk = voltdb.getHostMessenger().getZK();
    // network threads are blocked from making zookeeper calls
    Future<Long> fut = voltdb.getSES(true).submit(new Callable<Long>() {

        @Override
        public Long call() {
            try {
                Stat stat = zk.exists(VoltZK.operationMode, false);
                if (stat == null) {
                    VoltDB.crashLocalVoltDB("cluster operation mode zookeeper node does not exist");
                    return Long.MIN_VALUE;
                }
                return stat.getMzxid();
            } catch (KeeperException | InterruptedException e) {
                VoltDB.crashLocalVoltDB("Failed to stat the cluster operation zookeeper node", true, e);
                return Long.MIN_VALUE;
            }
        }
    });
    try {
        if (fut.get().longValue() != zkTxnId) {
            return unexpectedFailureResponse("Internal error: cannot write a startup snapshot because the " + "current system state is not consistent with an orderly shutdown. " + "Please try \"voltadmin shutdown --save\" again.", task.clientHandle);
        }
    } catch (InterruptedException | ExecutionException e1) {
        VoltDB.crashLocalVoltDB("Failed to stat the cluster operation zookeeper node", true, e1);
        return null;
    }
    NodeSettings paths = m_catalogContext.get().getNodeSettings();
    String data;
    try {
        data = new JSONStringer().object().keySymbolValuePair(SnapshotUtil.JSON_TERMINUS, zkTxnId).endObject().toString();
    } catch (JSONException e) {
        VoltDB.crashLocalVoltDB("Failed to create startup snapshot save command", true, e);
        return null;
    }
    log.info("Saving startup snapshot");
    consoleLog.info("Taking snapshot to save database contents");
    final SimpleClientResponseAdapter alternateAdapter = new SimpleClientResponseAdapter(ClientInterface.SHUTDONW_SAVE_CID, "Blocking Startup Snapshot Save");
    final InvocationClientHandler alternateHandler = new InvocationClientHandler() {

        @Override
        public boolean isAdmin() {
            return handler.isAdmin();
        }

        @Override
        public long connectionId() {
            return ClientInterface.SHUTDONW_SAVE_CID;
        }
    };
    final long sourceHandle = task.clientHandle;
    task.setClientHandle(alternateAdapter.registerCallback(SimpleClientResponseAdapter.NULL_CALLBACK));
    SnapshotUtil.SnapshotResponseHandler savCallback = new SnapshotUtil.SnapshotResponseHandler() {

        @Override
        public void handleResponse(ClientResponse r) {
            if (r == null) {
                String msg = "Snapshot save failed. The database is paused and the shutdown has been cancelled";
                transmitResponseMessage(gracefulFailureResponse(msg, sourceHandle), ccxn, sourceHandle);
            }
            if (r.getStatus() != ClientResponse.SUCCESS) {
                String msg = "Snapshot save failed: " + r.getStatusString() + ". The database is paused and the shutdown has been cancelled";
                ClientResponseImpl resp = new ClientResponseImpl(ClientResponse.GRACEFUL_FAILURE, r.getResults(), msg, sourceHandle);
                transmitResponseMessage(resp, ccxn, sourceHandle);
            }
            consoleLog.info("Snapshot taken successfully");
            task.setParams();
            dispatch(task, alternateHandler, alternateAdapter, user, bypass, false);
        }
    };
    // network threads are blocked from making zookeeper calls
    final byte[] guardContent = data.getBytes(StandardCharsets.UTF_8);
    Future<Boolean> guardFuture = voltdb.getSES(true).submit(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                ZKUtil.asyncMkdirs(zk, VoltZK.shutdown_save_guard, guardContent).get();
            } catch (NodeExistsException itIsOk) {
                return false;
            } catch (InterruptedException | KeeperException e) {
                VoltDB.crashLocalVoltDB("Failed to create shutdown save guard zookeeper node", true, e);
                return false;
            }
            return true;
        }
    });
    boolean created;
    try {
        created = guardFuture.get().booleanValue();
    } catch (InterruptedException | ExecutionException e) {
        VoltDB.crashLocalVoltDB("Failed to create shutdown save guard zookeeper node", true, e);
        return null;
    }
    if (!created) {
        return unexpectedFailureResponse("Internal error: detected concurrent invocations of \"voltadmin shutdown --save\"", task.clientHandle);
    }
    voltdb.getClientInterface().bindAdapter(alternateAdapter, null);
    SnapshotUtil.requestSnapshot(sourceHandle, paths.resolve(paths.getSnapshoth()).toPath().toUri().toString(), SnapshotUtil.getShutdownSaveNonce(zkTxnId), true, SnapshotFormat.NATIVE, SnapshotPathType.SNAP_AUTO, data, savCallback, true);
    return null;
}
Also used : ClientResponse(org.voltdb.client.ClientResponse) SnapshotUtil(org.voltdb.sysprocs.saverestore.SnapshotUtil) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) Stat(org.apache.zookeeper_voltpatches.data.Stat) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JSONStringer(org.json_voltpatches.JSONStringer) JSONException(org.json_voltpatches.JSONException) JSONException(org.json_voltpatches.JSONException) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NodeSettings(org.voltdb.settings.NodeSettings) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) JSONObject(org.json_voltpatches.JSONObject)

Example 2 with Stat

use of org.apache.zookeeper_voltpatches.data.Stat in project voltdb by VoltDB.

the class UpdateSettings method run.

public VoltTable[] run(SystemProcedureExecutionContext ctx, byte[] settingsBytes) {
    ZooKeeper zk = getHostMessenger().getZK();
    Stat stat = null;
    try {
        stat = zk.exists(VoltZK.cluster_settings, false);
    } catch (KeeperException | InterruptedException e) {
        String msg = "Failed to stat cluster settings zookeeper node";
        log.error(msg, e);
        throw new VoltAbortException(msg);
    }
    final int version = stat.getVersion();
    executeSysProcPlanFragments(createBarrierFragment(settingsBytes, version), DEP_updateSettingsBarrierAggregate);
    return executeSysProcPlanFragments(createUpdateFragment(settingsBytes, version), DEP_updateSettingsAggregate);
}
Also used : ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Stat(org.apache.zookeeper_voltpatches.data.Stat) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 3 with Stat

use of org.apache.zookeeper_voltpatches.data.Stat in project voltdb by VoltDB.

the class UpdateSettings method executePlanFragment.

@Override
public DependencyPair executePlanFragment(Map<Integer, List<VoltTable>> dependencies, long fragmentId, ParameterSet params, SystemProcedureExecutionContext context) {
    if (fragmentId == SysProcFragmentId.PF_updateSettingsBarrier) {
        DependencyPair success = new DependencyPair.TableDependencyPair(DEP_updateSettingsBarrier, new VoltTable(new ColumnInfo[] { new ColumnInfo("UNUSED", VoltType.BIGINT) }));
        if (log.isInfoEnabled()) {
            log.info("Site " + CoreUtils.hsIdToString(m_site.getCorrespondingSiteId()) + " reached settings update barrier.");
        }
        return success;
    } else if (fragmentId == SysProcFragmentId.PF_updateSettingsBarrierAggregate) {
        Object[] paramarr = params.toArray();
        byte[] settingsBytes = (byte[]) paramarr[0];
        int version = ((Integer) paramarr[1]).intValue();
        ZooKeeper zk = getHostMessenger().getZK();
        Stat stat = null;
        try {
            stat = zk.setData(VoltZK.cluster_settings, settingsBytes, version);
        } catch (KeeperException | InterruptedException e) {
            String msg = "Failed to update cluster settings";
            log.error(msg, e);
            throw new SettingsException(msg, e);
        }
        log.info("Saved new cluster settings state");
        return new DependencyPair.TableDependencyPair(DEP_updateSettingsBarrierAggregate, getVersionResponse(stat.getVersion()));
    } else if (fragmentId == SysProcFragmentId.PF_updateSettings) {
        Object[] paramarr = params.toArray();
        byte[] settingsBytes = (byte[]) paramarr[0];
        int version = ((Integer) paramarr[1]).intValue();
        ClusterSettings settings = ClusterSettings.create(settingsBytes);
        Pair<CatalogContext, CatalogSpecificPlanner> ctgdef = getVoltDB().settingsUpdate(settings, version);
        context.updateSettings(ctgdef.getFirst(), ctgdef.getSecond());
        VoltTable result = new VoltTable(VoltSystemProcedure.STATUS_SCHEMA);
        result.addRow(VoltSystemProcedure.STATUS_OK);
        return new DependencyPair.TableDependencyPair(DEP_updateSettings, result);
    } else if (fragmentId == SysProcFragmentId.PF_updateSettingsAggregate) {
        VoltTable result = VoltTableUtil.unionTables(dependencies.get(DEP_updateSettings));
        return new DependencyPair.TableDependencyPair(DEP_updateSettingsAggregate, result);
    } else {
        VoltDB.crashLocalVoltDB("Received unrecognized plan fragment id " + fragmentId + " in UpdateSettings", false, null);
    }
    throw new RuntimeException("Should not reach this code");
}
Also used : ClusterSettings(org.voltdb.settings.ClusterSettings) ColumnInfo(org.voltdb.VoltTable.ColumnInfo) VoltTable(org.voltdb.VoltTable) SettingsException(org.voltdb.settings.SettingsException) CatalogSpecificPlanner(org.voltdb.CatalogSpecificPlanner) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Stat(org.apache.zookeeper_voltpatches.data.Stat) CatalogContext(org.voltdb.CatalogContext) DependencyPair(org.voltdb.DependencyPair)

Example 4 with Stat

use of org.apache.zookeeper_voltpatches.data.Stat in project voltdb by VoltDB.

the class CoreZK method removeRejoinNodeIndicatorForHost.

/**
     * Removes the rejoin blocker if the current rejoin blocker contains the given host ID.
     * @return true if the blocker is removed successfully, false otherwise.
     */
public static boolean removeRejoinNodeIndicatorForHost(ZooKeeper zk, int hostId) {
    try {
        Stat stat = new Stat();
        final int rejoiningHost = ByteBuffer.wrap(zk.getData(rejoin_node_blocker, false, stat)).getInt();
        if (hostId == rejoiningHost) {
            zk.delete(rejoin_node_blocker, stat.getVersion());
            return true;
        }
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NONODE || e.code() == KeeperException.Code.BADVERSION) {
            // Okay if the rejoin blocker for the given hostId is already gone.
            return true;
        }
    } catch (InterruptedException e) {
        return false;
    }
    return false;
}
Also used : Stat(org.apache.zookeeper_voltpatches.data.Stat) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Example 5 with Stat

use of org.apache.zookeeper_voltpatches.data.Stat in project voltdb by VoltDB.

the class CoreZK method removeJoinNodeIndicatorForHost.

/**
     * Removes the join indicator for the given host ID.
     * @return true if the indicator is removed successfully, false otherwise.
     */
public static boolean removeJoinNodeIndicatorForHost(ZooKeeper zk, int hostId) {
    try {
        Stat stat = new Stat();
        String path = ZKUtil.joinZKPath(readyjoininghosts, Integer.toString(hostId));
        zk.getData(path, false, stat);
        zk.delete(path, stat.getVersion());
        return true;
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NONODE || e.code() == KeeperException.Code.BADVERSION) {
            // Okay if the join indicator for the given hostId is already gone.
            return true;
        }
    } catch (InterruptedException e) {
        return false;
    }
    return false;
}
Also used : Stat(org.apache.zookeeper_voltpatches.data.Stat) KeeperException(org.apache.zookeeper_voltpatches.KeeperException)

Aggregations

Stat (org.apache.zookeeper_voltpatches.data.Stat)30 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)15 ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)10 JSONException (org.json_voltpatches.JSONException)7 ExecutionException (java.util.concurrent.ExecutionException)5 JSONObject (org.json_voltpatches.JSONObject)5 List (java.util.List)4 BadVersionException (org.apache.zookeeper_voltpatches.KeeperException.BadVersionException)4 NoNodeException (org.apache.zookeeper_voltpatches.KeeperException.NoNodeException)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Code (org.apache.zookeeper_voltpatches.KeeperException.Code)3 VoltTable (org.voltdb.VoltTable)3 InetAddress (java.net.InetAddress)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 NodeExistsException (org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException)2