All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dasasian.chok.operation.node.AbstractShardOperation Maven / Gradle / Ivy

There is a newer version: 1.7
Show newest version
/**
 * Copyright (C) 2014 Dasasian ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dasasian.chok.operation.node;

import com.dasasian.chok.node.NodeContext;
import org.I0Itec.zkclient.ExceptionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public abstract class AbstractShardOperation implements NodeOperation {

    private static final long serialVersionUID = 1L;
    private final static Logger LOG = LoggerFactory.getLogger(AbstractShardOperation.class);

    private final Map shardUrisByShardNames = new LinkedHashMap<>(3);

    public Set getShardNames() {
        return shardUrisByShardNames.keySet();
    }

    public URI getShardUri(String shardName) {
        return shardUrisByShardNames.get(shardName);
    }

    public void addShard(String shardName, URI shardUri) {
        shardUrisByShardNames.put(shardName, shardUri);
    }

    public void addShard(String shardName) {
        shardUrisByShardNames.put(shardName, null);
    }

    @Override
    public final DeployResult execute(NodeContext context) throws InterruptedException {
        DeployResult result = new DeployResult(context.getNode().getName());
        for (String shardName : getShardNames()) {
            try {
                LOG.info(getOperationName() + " shard '" + shardName + "'");
                execute(context, shardName, result);
            } catch (Exception e) {
                ExceptionUtil.rethrowInterruptedException(e);
                LOG.error("failed to " + getOperationName() + " shard '" + shardName + "' on node '" + context.getNode().getName() + "'", e);
                result.addShardException(shardName, e);
                onException(context, shardName, e);
            }
        }
        return result;
    }

    protected abstract String getOperationName();

    protected abstract void execute(NodeContext context, String shardName, DeployResult result) throws Exception;

    protected abstract void onException(NodeContext context, String shardName, Exception e);

    protected void publishShard(String shardName, NodeContext context) {
        LOG.info("publish shard '" + shardName + "'");
        context.getProtocol().publishShard(context.getNode(), shardName);
    }

    @Override
    public final String toString() {
        return getClass().getSimpleName() + ":" + Integer.toHexString(hashCode()) + ":" + getShardNames();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy