/** * Build server ID from ConnectionInfo object. * * @param info ConnectionInfo * @return server ID in format thriftHost:thriftPort */ public static String getServerId(ConnectionInfo info) { return getServerId(KaaThriftService.OPERATIONS_SERVICE, info); }
/** * Create new instance of <code>NeighborConnection</code>. * * @param connectionInfo is connection info * @param maxNumberConnection os max number connection * @param socketTimeout is socket timeout * @param template is template */ public NeighborConnection(ConnectionInfo connectionInfo, int maxNumberConnection, long socketTimeout, T template) { this.connectionInfo = connectionInfo; this.maxNumberConnection = maxNumberConnection; this.socketTimeout = socketTimeout; this.template = template; this.id = Neighbors.getServerId(connectionInfo); }
public void setZkNode(KaaThriftService service, ConnectionInfo connectionInfo, WorkerNodeTracker zkNode) { setZkNode(getServerId(service, connectionInfo), zkNode); }
@Override public void onNodeRemoved(OperationsNodeInfo nodeInfo) { String opId = getServerId(nodeInfo.getConnectionInfo()); if (!zkId.equals(opId)) { NeighborConnection<T, V> connection = neigbors.remove(opId); if (connection != null) { connection.shutdown(); } LOG.info("Operations server {} removed to {} Neighbors list ({}). Now {} neighbors", opId, neigbors.size()); } } });
/** * Create connection and send messages. * * @param info is information about connection * @param msgs <code>Collection</code> of messages */ public void sendMessages(ConnectionInfo info, Collection<V> msgs) { NeighborConnection<T, V> neighbor = neigbors.get(getServerId(info)); if (neighbor != null) { try { neighbor.sendMessages(msgs); } catch (InterruptedException ex) { LOG.error("Failed to send message to {}", neighbor.getId()); throw new RuntimeException(ex); } } else { LOG.warn("Can't find server for id {}", getServerId(info)); } }
@Override public String getUserNode(String userId) { OperationsNodeInfo info = resolver.getNode(userId); if (info != null) { return Neighbors.getServerId(info.getConnectionInfo()); } return null; }
@Override public boolean isMainEntityNode(byte[] entityId) { String entityIdStr = Base64Util.encode(entityId); OperationsNodeInfo info = resolver.getNode(entityIdStr); if (info == null) { return false; } String nodeId = Neighbors.getServerId(info.getConnectionInfo()); LOG.trace("Comparing {} to {} for entity {}", id, nodeId, entityIdStr); return id.equals(nodeId); }
@Override public boolean isMainUserNode(String userId) { OperationsNodeInfo info = resolver.getNode(userId); if (info == null) { return false; } LOG.trace("comparing {} to {} for user {}", id, info.getConnectionInfo(), userId); return id.equals(Neighbors.getServerId(info.getConnectionInfo())); }
private void addOpsServer(OperationsNodeInfo opServer) { LOG.trace("[{}] Building id for {}", zkId, opServer.getConnectionInfo()); String opId = getServerId(serviceType, opServer.getConnectionInfo()); if (!zkId.equals(opId)) { LOG.trace("Adding {} to {}", opId, neigbors); neigbors.putIfAbsent(opId, new NeighborConnection<T, V>(opServer.getConnectionInfo(), maxNumberNeighborConnections, template)); neigbors.get(opId).start(); LOG.info("Operations server {} added/updated to {} Neighbors list. Now {} neighbors", opId, zkId, neigbors.size()); } } }
@Override public String getEntityNode(byte[] entityId) { OperationsNodeInfo info = resolver.getNode(Base64Util.encode(entityId)); if (info != null) { return Neighbors.getServerId(info.getConnectionInfo()); } return null; }
@Override public void sendEndpointRouteInfo(GlobalRouteInfo routeInfo) { LOG.trace("calculating server for user {}", routeInfo.getUserId()); String serverId = Neighbors.getServerId( resolver.getNode(routeInfo.getUserId()).getConnectionInfo()); sendMessagesToServer(packMessage(routeInfo), serverId); }
@Override public void setZkNode(OperationsNode operationsNode) { this.operationsNode = operationsNode; this.id = Neighbors.getServerId(this.operationsNode.getNodeInfo().getConnectionInfo()); neighbors.setZkNode( KaaThriftService.OPERATIONS_SERVICE, this.operationsNode.getNodeInfo().getConnectionInfo(), operationsNode); if (resolver != null) { updateResolver(this.resolver); } }
@Override public void setZkNode(OperationsNode operationsNode) { this.operationsNode = operationsNode; this.id = Neighbors.getServerId( KaaThriftService.OPERATIONS_SERVICE, this.operationsNode.getNodeInfo().getConnectionInfo()); neighbors.setZkNode( KaaThriftService.OPERATIONS_SERVICE, this.operationsNode.getNodeInfo().getConnectionInfo(), operationsNode); if (resolver != null) { updateResolver(this.resolver); } }
if (LOG.isTraceEnabled()) { LOG.trace("Sending message {} to [{}]", nf, Neighbors.getServerId(server.getConnectionInfo()));
private void sendEndpointConfigurationRefreshMessage(EndpointSpecificConfigurationDto configuration) { byte[] endpointKeyHashBytes = configuration.getEndpointKeyHash(); EndpointProfileDto endpointProfile = endpointService.findEndpointProfileByKeyHash(endpointKeyHashBytes); if (!configuration.getConfigurationSchemaVersion().equals(endpointProfile.getConfigurationVersion())) { return; } checkNeighbors(); String endpointKeyHash = Base64Util.encode(configuration.getEndpointKeyHash()); ApplicationDto appDto = applicationService.findAppById(endpointProfile.getApplicationId()); OperationsNodeInfo server = resolve(endpointKeyHash); if (server != null) { ThriftEndpointConfigurationRefreshMessage msg = new ThriftEndpointConfigurationRefreshMessage(); msg.setAddress(new ThriftEntityAddress(appDto.getTenantId(), appDto.getApplicationToken(), ThriftClusterEntityType.ENDPOINT, ByteBuffer.wrap(endpointKeyHashBytes))); msg.setActorClassifier(ThriftActorClassifier.GLOBAL); if (LOG.isTraceEnabled()) { LOG.trace("Sending message {} to [{}]", msg, Neighbors.getServerId(server.getConnectionInfo())); } neighbors.sendMessage(server.getConnectionInfo(), OperationsServiceMsg.fromEndpointConfigurationRefresh(msg)); } else { LOG.warn("Can't find server for endpoint [{}]", endpointKeyHash); } }
if (LOG.isTraceEnabled()) { LOG.trace("Sending message {} to [{}]", nf, Neighbors.getServerId(server.getConnectionInfo()));
@Override public void editUserConfiguration(EndpointUserConfigurationDto configuration) throws ControlServiceException { ApplicationDto appDto = applicationService.findAppByApplicationToken( configuration.getAppToken()); EndpointUserDto userDto = endpointService.findEndpointUserByExternalIdAndTenantId( configuration.getUserId(), appDto.getTenantId()); if (userDto == null) { throw new NotFoundException("Specified user not found!"); } configuration.setUserId(userDto.getId()); configuration = userConfigurationService.saveUserConfiguration(configuration); EndpointObjectHash hash = EndpointObjectHash.fromString(configuration.getBody()); checkNeighbors(); OperationsNodeInfo server = resolve(configuration.getUserId()); if (server != null) { UserConfigurationUpdate msg = new UserConfigurationUpdate(appDto.getTenantId(), configuration.getUserId(), configuration.getAppToken(), configuration.getSchemaVersion(), hash.getDataBuf()); if (LOG.isTraceEnabled()) { LOG.trace("Sending message {} to [{}]", msg, Neighbors.getServerId( server.getConnectionInfo())); } neighbors.sendMessage(server.getConnectionInfo(), OperationsServiceMsg.fromUpdate(msg)); } else { LOG.warn("Can't find server for user [{}]", configuration.getUserId()); } }