- Add the Codota plugin to your IDE and get smart completions
private void myMethod () {}
private Set<NetEndpoint> getCandidateEndpoints() { return getCandidateEndpoints(db, db.getHostIds()); }
private static AbstractEndpointAssignmentStrategy getEndpointAssignmentStrategy(IDatabase db) { if (db.getEndpointAssignmentProperties() == null) return defaultEndpointAssignmentStrategy; AbstractEndpointAssignmentStrategy endpointAssignmentStrategy = endpointAssignmentStrategies.get(db); if (endpointAssignmentStrategy == null) { HashMap<String, String> map = new HashMap<>(db.getEndpointAssignmentProperties()); String className = map.remove("class"); if (className == null) { throw new ConfigException("Missing endpoint assignment strategy class"); } endpointAssignmentStrategy = AbstractEndpointAssignmentStrategy.create(db.getShortName(), className, ConfigDescriptor.getEndpointSnitch(), map); endpointAssignmentStrategies.put(db, endpointAssignmentStrategy); } return endpointAssignmentStrategy; } }
boolean isShardingMode() { if (runMode != null) { return runMode == RunMode.SHARDING; } return db.isShardingMode(); }
private static AbstractReplicationStrategy getReplicationStrategy(IDatabase db) { if (db.getReplicationProperties() == null) return defaultReplicationStrategy; AbstractReplicationStrategy replicationStrategy = replicationStrategies.get(db); if (replicationStrategy == null) { HashMap<String, String> map = new HashMap<>(db.getReplicationProperties()); String className = map.remove("class"); if (className == null) { throw new ConfigException("Missing replication strategy class"); } replicationStrategy = AbstractReplicationStrategy.createReplicationStrategy(db.getShortName(), className, ConfigDescriptor.getEndpointSnitch(), map); replicationStrategies.put(db, replicationStrategy); } return replicationStrategy; }
Session currentSession = defineStatement.getSession(); IDatabase db = currentSession.getDatabase(); String[] hostIds = db.getHostIds(); if (hostIds.length == 0) { throw DbException .throwInternalError("DB: " + db.getShortName() + ", Run Mode: " + db.getRunMode() + ", no hostIds"); } else { liveMembers = new HashSet<>(hostIds.length); if (defineStatement.isReplicationStatement() && db.isShardingMode() && !db.isStarting()) { List<NetEndpoint> endpoints = m.getReplicationEndpoints(db, new HashSet<>(0), liveMembers); if (!endpoints.isEmpty()) {
private void replicateRootPages(Object dbObject, String[] oldEndpoints, String[] targetEndpoints, RunMode runMode) { AOStorageService.forceMerge(); List<NetEndpoint> replicationEndpoints = getReplicationEndpoints(targetEndpoints); // 用最高权限的用户移动页面,因为目标节点上可能还没有对应的数据库 IDatabase db = (IDatabase) dbObject; Session session = db.createInternalSession(true); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); int id = db.getId(); String sysMapName = "t_" + id + "_0"; try (DataBuffer p = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { HashMap<String, StorageMap<?, ?>> maps = new HashMap<>(this.maps); Collection<StorageMap<?, ?>> values = maps.values(); p.putInt(values.size()); // SYS表放在前面,并且总是使用CLIENT_SERVER模式 StorageMap<?, ?> sysMap = maps.remove(sysMapName); replicateRootPage(db, sysMap, p, oldEndpoints, RunMode.CLIENT_SERVER); for (StorageMap<?, ?> map : values) { replicateRootPage(db, map, p, oldEndpoints, runMode); } ByteBuffer pageBuffer = p.getAndFlipBuffer(); c.replicateRootPages(db.getShortName(), pageBuffer); db.notifyRunModeChanged(); } }
@Override public String[] getShardingEndpoints(IDatabase db) { HashSet<NetEndpoint> oldEndpoints = new HashSet<>(); for (String hostId : db.getHostIds()) { oldEndpoints.add(P2pServer.instance.getTopologyMetaData().getEndpoint(hostId)); } Set<NetEndpoint> liveMembers = Gossiper.instance.getLiveMembers(); liveMembers.removeAll(oldEndpoints); ArrayList<NetEndpoint> list = new ArrayList<>(liveMembers); int size = liveMembers.size(); AbstractReplicationStrategy replicationStrategy = getReplicationStrategy(db); int replicationFactor = replicationStrategy.getReplicationFactor(); Map<String, String> parameters = db.getParameters(); int nodes = replicationFactor + 2; if (parameters != null && parameters.containsKey("nodes")) { nodes = Integer.parseInt(parameters.get("nodes")); } nodes -= db.getHostIds().length; return getHostIds(list, size, nodes); }
private void removeLeafPage(PageKey pageKey, BTreePage leafPage) { if (leafPage.getReplicationHostIds().get(0).equals(getLocalHostId())) { AOStorageService.submitTask(() -> { List<NetEndpoint> oldReplicationEndpoints = getReplicationEndpoints(leafPage); Set<NetEndpoint> otherEndpoints = getCandidateEndpoints(); otherEndpoints.removeAll(oldReplicationEndpoints); Session session = db.createInternalSession(); ReplicationSession rs = db.createReplicationSession(session, otherEndpoints, true); try (StorageCommand c = rs.createStorageCommand()) { c.removeLeafPage(getName(), pageKey); } }); } }
public String[] getHostIdsOld(IDatabase db) { RunMode runMode = db.getRunMode(); Set<NetEndpoint> liveMembers = Gossiper.instance.getLiveMembers(); ArrayList<NetEndpoint> list = new ArrayList<>(liveMembers); int size = liveMembers.size(); if (runMode == RunMode.CLIENT_SERVER) { int i = random.nextInt(size); NetEndpoint addr = list.get(i); return new String[] { getHostId(addr) }; } else if (runMode == RunMode.REPLICATION) { AbstractReplicationStrategy replicationStrategy = getReplicationStrategy(db); int replicationFactor = replicationStrategy.getReplicationFactor(); return getHostIds(list, size, replicationFactor); } else if (runMode == RunMode.SHARDING) { AbstractReplicationStrategy replicationStrategy = getReplicationStrategy(db); int replicationFactor = replicationStrategy.getReplicationFactor(); Map<String, String> parameters = db.getParameters(); int nodes = replicationFactor + 2; if (parameters != null && parameters.containsKey("nodes")) { nodes = Integer.parseInt(parameters.get("nodes")); } return getHostIds(list, size, nodes); } return new String[0]; }
List<NetEndpoint> newReplicationEndpoints = db.getReplicationEndpoints(oldEndpointSet, candidateEndpoints); Session session = db.createInternalSession(); LeafPageMovePlan leafPageMovePlan = null; p.setLeafPageMovePlan(leafPageMovePlan); } else { ReplicationSession rs = db.createReplicationSession(session, oldReplicationEndpoints); try (StorageCommand c = rs.createStorageCommand()) { LeafPageMovePlan plan = new LeafPageMovePlan(getLocalHostId(), newReplicationEndpoints, pageKey); ReplicationSession rs = db.createReplicationSession(session, newReplicationEndpoints, true); moveLeafPage(leafPageMovePlan.pageKey, p, rs, false, !replicate); ReplicationSession rs = db.createReplicationSession(session, otherEndpoints, true); moveLeafPage(leafPageMovePlan.pageKey, p, rs, true, !replicate);
@Override public Object replicationGet(Session session, Object key) { List<NetEndpoint> replicationEndpoints = getReplicationEndpoints(key); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); try (DataBuffer k = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); byte[] value = (byte[]) c.executeGet(getName(), keyBuffer); if (value == null) return null; return valueType.read(ByteBuffer.wrap(value)); } }
static Set<NetEndpoint> getCandidateEndpoints(IDatabase db, String[] hostIds) { Set<NetEndpoint> candidateEndpoints = new HashSet<>(hostIds.length); for (String hostId : hostIds) { candidateEndpoints.add(db.getEndpoint(hostId)); } return candidateEndpoints; }
switch (statement.getType()) { case SQLStatement.CREATE_DATABASE: sql = db.getCreateSQL(); break; case SQLStatement.DROP_DATABASE:
synchronized BTreePage readRemotePage(BTreeMap<Object, Object> map) { if (page != null) { return page; } // TODO 支持多节点容错 String remoteHostId = replicationHostIds.get(0); List<NetEndpoint> replicationEndpoints = BTreeMap.getReplicationEndpoints(map.db, new String[] { remoteHostId }); Session session = map.db.createInternalSession(true); ReplicationSession rs = map.db.createReplicationSession(session, replicationEndpoints); try (StorageCommand c = rs.createStorageCommand()) { ByteBuffer pageBuffer = c.readRemotePage(map.getName(), pageKey); page = BTreePage.readReplicatedPage(map, pageBuffer); } if (!map.isShardingMode() || (page.getReplicationHostIds() != null && page.getReplicationHostIds().contains(NetEndpoint.getLocalTcpHostAndPort()))) { pos = 0; } return page; }
@Override public Object replicationAppend(Session session, Object value, StorageDataType valueType) { List<NetEndpoint> replicationEndpoints = getLastPageReplicationEndpoints(); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); try (DataBuffer v = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer valueBuffer = v.write(valueType, value); return c.executeAppend(null, getName(), valueBuffer, null); } }
static List<NetEndpoint> getReplicationEndpoints(IDatabase db, List<String> replicationHostIds) { int size = replicationHostIds.size(); List<NetEndpoint> replicationEndpoints = new ArrayList<>(size); for (int i = 0; i < size; i++) { replicationEndpoints.add(db.getEndpoint(replicationHostIds.get(i))); } return replicationEndpoints; }
@Override public String[] getReplicationEndpoints(IDatabase db) { removeReplicationStrategy(db); // 避免使用旧的 String[] oldHostIds = db.getHostIds(); int size = oldHostIds.length; List<NetEndpoint> oldReplicationEndpoints = new ArrayList<>(size); for (int i = 0; i < size; i++) { oldReplicationEndpoints.add(P2pServer.instance.getTopologyMetaData().getEndpoint(oldHostIds[i])); } List<NetEndpoint> newReplicationEndpoints = getLiveReplicationEndpoints(db, new HashSet<>(oldReplicationEndpoints), Gossiper.instance.getLiveMembers(), true); size = newReplicationEndpoints.size(); String[] hostIds = new String[size]; int j = 0; for (NetEndpoint e : newReplicationEndpoints) { String hostId = getHostId(e); if (hostId != null) hostIds[j++] = hostId; } return hostIds; }
private Object putRemote(BTreePage p, Object key, Object value) { if (p.getLeafPageMovePlan().moverHostId.equals(getLocalHostId())) { int size = p.getLeafPageMovePlan().replicationEndpoints.size(); List<NetEndpoint> replicationEndpoints = new ArrayList<>(size); replicationEndpoints.addAll(p.getLeafPageMovePlan().replicationEndpoints); boolean containsLocalEndpoint = replicationEndpoints.remove(getLocalEndpoint()); Object returnValue = null; ReplicationSession rs = db.createReplicationSession(db.createInternalSession(), replicationEndpoints); try (DataBuffer k = DataBuffer.create(); DataBuffer v = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); ByteBuffer valueBuffer = v.write(valueType, value); byte[] oldValue = (byte[]) c.executePut(null, getName(), keyBuffer, valueBuffer, true); if (oldValue != null) { returnValue = valueType.read(ByteBuffer.wrap(oldValue)); } } // 如果新的复制节点中还包含本地节点,那么还需要put到本地节点中 if (containsLocalEndpoint) { return putLocal(p, key, value); } else { return returnValue; } } else { return null; // 不是由当前节点移动的,那么put操作就可以忽略了 } }
@Override public Object replicationPut(Session session, Object key, Object value, StorageDataType valueType) { List<NetEndpoint> replicationEndpoints = getReplicationEndpoints(key); ReplicationSession rs = db.createReplicationSession(session, replicationEndpoints); try (DataBuffer k = DataBuffer.create(); DataBuffer v = DataBuffer.create(); StorageCommand c = rs.createStorageCommand()) { ByteBuffer keyBuffer = k.write(keyType, key); ByteBuffer valueBuffer = v.write(valueType, value); byte[] oldValue = (byte[]) c.executePut(null, getName(), keyBuffer, valueBuffer, false); if (oldValue == null) return null; return valueType.read(ByteBuffer.wrap(oldValue)); } }
private List<NetEndpoint> getReplicationEndpoints(List<String> replicationHostIds) { int size = replicationHostIds.size(); List<NetEndpoint> replicationEndpoints = new ArrayList<>(size); for (int i = 0; i < size; i++) { replicationEndpoints.add(db.getEndpoint(replicationHostIds.get(i))); } return replicationEndpoints; }