private Set<String> getAllHostsAtSite(String siteId) throws IOException { Set<String> allHosts = config.getHostIds().stream() .filter(hostId -> siteId.equals(config.getAttributesForHost(hostId).get("siteId"))) .collect(Collectors.toSet()); if (allHosts.isEmpty()) { throw new IOException("SiteID " + siteId + " does not exist"); } return allHosts; } }
private Set<String> getRelatedHostIds() { return myHostIds.stream() .map(hostId -> config.getShardIds(hostId)) .flatMap(Collection::stream) .distinct() .map(shardId -> config.getMembersInShard(shardId)) .flatMap(Collection::stream) .map(Config.ConfigMember::getHostId) .collect(Collectors.toSet()); }
public void update(Observable obs, Object arg) { Config config = (Config) arg; networkTracing = config.getBoolean("tracing.network"); createSocketRetryPeriod = config.getInt("network_socket.create_socket_retry_period"); heartbeatPeriod = config.getInt("raft.heartbeat_period"); connTimeout = config.getInt("network_socket.connect_timeout"); }
/** * Must be called before logger objects are created. */ public static void initConfig(Config config) { config.registerForUpdates(config1 -> { stackTraceSuppression = config1.getBoolean("gondola.stack_trace_suppression"); } ); }
/** * Must be called before command objects are created. */ public static void initConfig(Config config) { config.registerForUpdates(config1 -> { commandTracing = config1.getBoolean("gondola.tracing.command"); maxCommandSize = config1.getInt("raft.command_max_size"); }); }
/** * A compatibility checking tool for routing module. * * @param config the config */ public static void configCheck(Config config) { StringBuilder sb = new StringBuilder(); for (String shardId : config.getShardIds()) { if (!config.getAttributesForShard(shardId).containsKey("bucketMap")) { sb.append("Shard bucketMap attribute is missing on Shard - ").append(shardId).append("\n"); } } for (String hostId : config.getHostIds()) { Map<String, String> attributes = config.getAttributesForHost(hostId); if (!attributes.containsKey("appScheme") || !attributes.containsKey("appPort")) { sb.append("Host attributes appScheme and appPort is missing on Host - ").append(hostId).append("\n"); } } if (!sb.toString().isEmpty()) { throw new IllegalStateException("Configuration Error: " + sb.toString()); } }
private Map getConfigInfo(Gondola gondola) { Map<Object, Object> map = new LinkedHashMap<>(); Config config = gondola.getConfig(); map.put("file", config.getFile()); map.put("members", config.getMembers()); map.put("shards", config.getShardIds().stream() .map(config::getAttributesForShard) .collect(Collectors.toList())); map.put("hosts", config.getHostIds().stream() .map(config::getAttributesForHost) .collect(Collectors.toList())); return map; }
switch (target) { case HOST: for (String shardId : config.getShardIds(targetId)) { gondolaAdminClient.enable(targetId, shardId, enable); throw new UnsupportedOperationException("Enable/disable a shard is not allowed."); case SITE: config.getHostIds().stream().filter(hostId -> config.getSiteIdForHost(hostId).equals(targetId)).forEach( hostId -> { for (String shardId : config.getShardIds(hostId)) { gondolaAdminClient.enable(hostId, shardId, enable);
public SocketNetwork(Gondola gondola, String hostId) throws SocketException { this.gondola = gondola; this.hostId = hostId; gondola.getConfig().registerForUpdates(config -> { networkTracing = config.getBoolean("gondola.tracing.network"); connTimeout = config.getInt("network.socket.connect_timeout"); }); InetSocketAddress address = gondola.getConfig().getAddressForHost(hostId); if (!isLocalAddress(address.getAddress())) { throw new IllegalStateException(address.getHostName() + " is not a local address"); } // Check whether another process is already using this address if (isActive(getAddress())) { throw new IllegalStateException(String.format("Another process is actively listening to %s", getAddress())); } }
public MySqlStorage(Gondola gondola, String hostId) throws GondolaException { maxCommandSize = cfg.getInt("raft.command_max_size"); String user = cfg.get("storage.mysql.user"); String password = cfg.get("storage.mysql.password"); String url = cfg.get("storage.mysql.url"); String storeId = cfg.getAttributesForHost(hostId).get("storeId"); if (storeId != null) { String urlKey = "storage." + storeId + ".mysql.url"; if (cfg.has(urlKey)) { url = cfg.get(urlKey);
URI uri = URI.create(argsList.get(1)); Config configInstance = ConfigLoader.getConfigInstance(uri); adminClient.setConfig(configInstance.getFile()); break; case "getConfig": System.out.println(Files.readAllLines(Paths.get(adminClient.getConfig().getFile().getAbsolutePath()))); break; case "enableTracing": usage(); adminClient.inspectRequestUri(config.getHostIds().get(0), argsList.get(1)); break; case "setLeader": for (String shardId : config.getShardIds(hostId)) { adminClient.setLeader(hostId, shardId);
void createConnection() throws GondolaException { Config cfg = gondola.getConfig(); String user = cfg.get("storage.h2.user"); String password = cfg.get("storage.h2.password"); // If there's a store-specific setting, use it; otherwise use default String url = cfg.get("storage.h2.url"); String storeId = cfg.getAttributesForHost(hostId).get("storeId"); if (storeId != null) { String urlKey = "storage." + storeId + ".h2.url"; if (cfg.has(urlKey)) { url = cfg.get(urlKey); } } url = url.replace("$hostId", hostId); logger.info("Initializing H2DB storage. maxCommandSize={} url={} user={}", maxCommandSize, url, user); try { c = DriverManager.getConnection(url, user, password); c.setAutoCommit(true); } catch (SQLException e) { throw new GondolaException(e); } }
/** * * @param gondola * @param memberId * @param toMemberId */ public SocketChannel(Gondola gondola, int memberId, int toMemberId) { this.gondola = gondola; this.memberId = memberId; this.peerId = toMemberId; gondola.getConfig().registerForUpdates(config -> { networkTracing = config.getBoolean("gondola.tracing.network"); createSocketRetryPeriod = config.getInt("network.socket.create_socket_retry_period"); heartbeatPeriod = config.getInt("raft.heartbeat_period"); connTimeout = config.getInt("network.socket.connect_timeout"); }); if (networkTracing) { logger.info("[{}-{}] Creating connection to {} {}", gondola.getHostId(), memberId, toMemberId, this.toString()); } inetSocketAddress = gondola.getConfig().getAddressForMember(peerId); }
/** * Private methods. */ private void loadRoutingTable() { // The routing entry will be modified on the fly, concurrent map is needed Map<String, List<String>> newRoutingTable = new ConcurrentHashMap<>(); Config config = gondola.getConfig(); for (String hostId : config.getHostIds()) { if (hostId.equals(gondola.getHostId())) { myAppUri = Utils.getAppUri(config, hostId); continue; } String appUri = Utils.getAppUri(config, hostId); serviceUris.put(hostId, appUri); for (String shardId : config.getShardIds(hostId)) { List<String> addresses = newRoutingTable.get(shardId); if (addresses == null) { addresses = new ArrayList<>(); newRoutingTable.put(shardId, addresses); } addresses.add(appUri); } } routingTable = newRoutingTable; }
public void update(Observable obs, Object arg) { Config config = (Config) arg; storageTracing = config.getBoolean("tracing.storage"); commandTracing = config.getBoolean("tracing.command"); heartbeatPeriod = config.getInt("raft.heartbeat_period"); requestVotePeriod = config.getInt("raft.request_vote_period"); summaryTracingPeriod = config.getInt("tracing.summary_period"); electionTimeout = config.getInt("raft.election_timeout"); leaderTimeout = config.getInt("raft.leader_timeout"); incomingQueueSize = config.getInt("gondola.incoming_queue_size"); waitQueueThrottleSize = config.getInt("gondola.wait_queue_throttle_size"); fileLockDir = new File(config.get("gondola.file_lock_dir")); // Some validations if (heartbeatPeriod >= electionTimeout) { throw new IllegalStateException(String.format("heartbeat period (%d) must be < election timeout (%d)", heartbeatPeriod, electionTimeout)); } }
public static String getAppUri(Config config, String hostId) { InetSocketAddress address = config.getAddressForHost(hostId); Map<String, String> attrs = config.getAttributesForHost(hostId); String appUri = String.format("%s://%s:%s", attrs.get(APP_SCHEME), address.getHostName(), attrs.get(APP_PORT)); if (!attrs.containsKey(APP_PORT) || !attrs.containsKey(APP_SCHEME)) { throw new IllegalStateException( String .format("gondola.hosts[%s] is missing either the %s or %s config values", hostId, APP_PORT, APP_SCHEME)); } return appUri; }
private void init() throws Exception { // Create implementations String clockClassName = config.get(config.get("clock.impl") + ".class"); clock = (Clock) Class.forName(clockClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); String networkClassName = config.get(config.get("network.impl") + ".class"); network = (Network) Class.forName(networkClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); String storageClassName = config.get(config.get("storage.impl") + ".class"); storage = (Storage) Class.forName(storageClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); // Create the shards running on a host for (String shardId : config.getShardIds(hostId)) { Shard shard = new Shard(this, shardId); shards.add(shard); shardMap.put(shardId, shard); } inited = true; }
private Gondola createGondolaInstance() throws GondolaException { Config config = ConfigLoader.getConfigInstance(configUri); String hostId = null; for (String h : config.getHostIds()) { if (Utils.isMyAddress(config.getAddressForHost(h).getAddress())) { hostId = h; break; } } if (System.getenv("hostId") != null) { hostId = System.getenv("hostId"); } if (hostId == null) { throw new IllegalStateException("Cannot find IP address on the host."); } return new Gondola(config, hostId); }
this.config = config; this.hostId = hostId; logger.info("------- Gondola init: {}, {}, pid={} -------", hostId, config.getAddressForHost(hostId), processId); String clockClassName = config.get(config.get("clock.impl") + ".class"); clock = (Clock) Class.forName(clockClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); String networkClassName = config.get(config.get("network.impl") + ".class"); network = (Network) Class.forName(networkClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); String storageClassName = config.get(config.get("storage.impl") + ".class"); storage = (Storage) Class.forName(storageClassName).getConstructor(Gondola.class, String.class) .newInstance(this, hostId); List<String> clusterIds = config.getClusterIds(hostId); for (String clusterId : clusterIds) { Cluster cluster = new Cluster(this, clusterId);