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

com.dasasian.chok.client.DefaultNodeSelectionPolicy 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.client;

import com.dasasian.chok.util.CircularList;
import com.google.common.collect.ImmutableSetMultimap;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultNodeSelectionPolicy implements INodeSelectionPolicy {

    private final Map> shardsToNodeMap = new ConcurrentHashMap<>();

    @Override
    public void update(String shard, Iterable nodes) {
        shardsToNodeMap.put(shard, new CircularList<>(nodes));
    }

    @Override
    public Collection getShardNodes(String shard) {
        CircularList nodeList = shardsToNodeMap.get(shard);
        if (nodeList == null) {
            return Collections.emptyList();
        }
        return nodeList.asList();
    }

    @Override
    public List remove(String shard) {
        CircularList nodes = shardsToNodeMap.remove(shard);
        if (nodes == null) {
            return Collections.emptyList();
        }
        return nodes.asList();
    }

    @Override
    public void removeNode(String node) {
        Set shards = shardsToNodeMap.keySet();
        for (String shard : shards) {
            CircularList nodes = shardsToNodeMap.get(shard);
            synchronized (nodes) {
                nodes.remove(node);
            }
        }
    }

    @Override
    public ImmutableSetMultimap createNode2ShardsMap(Iterable shards) throws ShardAccessException {
        ImmutableSetMultimap.Builder node2ShardsMapBuilder = ImmutableSetMultimap.builder();
        for (String shard : shards) {
            CircularList nodeList = shardsToNodeMap.get(shard);
            if (nodeList == null || nodeList.isEmpty()) {
                throw new ShardAccessException(shard);
            }
            String node;
            synchronized (nodeList) {
                node = nodeList.getNext();
            }
            node2ShardsMapBuilder.put(node, shard);
        }
        return node2ShardsMapBuilder.build();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("DefaultNodeSelectionPolicy: ");
        String sep = "";
        for (Map.Entry> e : shardsToNodeMap.entrySet()) {
            builder.append(sep);
            builder.append(e.getKey());
            builder.append(" --> ");
            builder.append(e.getValue());
            sep = " ";
        }
        return builder.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy