Java tutorial
/** Copyright 2014 OpenSearchServer, Inc. * http://www.opensearchserver.com * * 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.opensearchserver.hadse.cluster; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.opensearchserver.hadse.Hadse; import com.opensearchserver.hadse.utils.JsonUtils; import com.opensearchserver.hadse.utils.NetworksUtils; import com.opensearchserver.hadse.utils.ReadWriteLock; public class ClusterCatalog { private final static ReadWriteLock lock = new ReadWriteLock(); private final static List<NodeItem> nodes = new ArrayList<NodeItem>(1); private final static Map<String, NodeItem> addressMap = new TreeMap<String, NodeItem>(); private final static File clusterFile; private final static Set<String> localAddresses = new TreeSet<String>(); public static NodeItem localNode = null; static { clusterFile = Hadse.data_dir != null ? new File(Hadse.data_dir, "cluster.json") : null; } public final static void init(int port) throws JsonGenerationException, JsonMappingException, URISyntaxException, IOException { List<String> addrList = new ArrayList<String>(); NetworksUtils.collectExternalIpAdresses(addrList); localAddresses.clear(); if (addrList != null) for (String addr : addrList) localAddresses.add(addr + ":" + port); List<NodeItem> nodeItems = null; if (clusterFile.exists()) { try { nodeItems = JsonUtils.mapper.readValue(clusterFile, new TypeReference<ArrayList<NodeItem>>() { }); } catch (Throwable t) { throw new RuntimeException(t); } if (nodeItems != null) for (NodeItem nodeItem : nodeItems) { nodes.add(nodeItem); for (String addr : nodeItem.getAddresses()) addressMap.put(addr, nodeItem); } } add(localAddresses); } final public static boolean isLocal(String address) { return localAddresses.contains(address); } final static HttpEntity<?> add(Collection<String> addresses) throws URISyntaxException, JsonGenerationException, JsonMappingException, IOException { lock.w.lock(); try { for (String addr : addresses) if (addressMap.containsKey(addr)) return new ResponseEntity<String>("The address is already registered: " + addr, HttpStatus.ALREADY_REPORTED); NodeItem newNode = new NodeItem(addresses); nodes.add(newNode); for (String ad : addresses) { if (isLocal(ad)) { localNode = newNode; break; } } JsonUtils.mapper.writeValue(clusterFile, nodes); return new ResponseEntity<String>("Node registered", HttpStatus.ACCEPTED); } finally { lock.w.unlock(); } } public final static Collection<NodeItem> get() { lock.r.lock(); try { return nodes; } finally { lock.r.unlock(); } } public final static NodeItem nextShardCandidate() { // TODO return localNode; } }