private Map<TransportProtocolId, List<TransportConnectionInfo>> parseBootstrapServers( String serversStr) throws InvalidKeySpecException, NoSuchAlgorithmException { Map<TransportProtocolId, List<TransportConnectionInfo>> servers = new HashMap<>(); String[] serversSplit = serversStr.split(";"); for (String server : serversSplit) { if (server != null && !server.trim().isEmpty()) { String[] tokens = server.split(":"); ProtocolMetaData md = new ProtocolMetaData(); md.setAccessPointId(Integer.valueOf(tokens[0])); md.setProtocolVersionInfo(new ProtocolVersionPair(Integer.valueOf(tokens[1]), Integer.valueOf(tokens[2]))); md.setConnectionInfo(ByteBuffer.wrap(getBase64().decodeBase64(tokens[3]))); TransportProtocolId key = new TransportProtocolId(md.getProtocolVersionInfo().getId(), md.getProtocolVersionInfo().getVersion()); List<TransportConnectionInfo> serverList = servers.get(key); if (serverList == null) { serverList = new ArrayList<TransportConnectionInfo>(); servers.put(key, serverList); } serverList.add(new GenericTransportInfo(ServerType.BOOTSTRAP, md)); } } return servers; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof GenericTransportInfo)) { return false; } GenericTransportInfo that = (GenericTransportInfo) obj; if (md != null ? !md.equals(that.md) : that.md != null) { return false; } if (serverType != that.serverType) { return false; } if (transportId != null ? !transportId.equals(that.transportId) : that.transportId != null) { return false; } return true; }
@Override public int getAccessPointId() { return md.getAccessPointId(); }
private static List<ProtocolMetaData> convert(Set<ProtocolConnectionData> source) { if (source == null) { return Collections.emptyList(); } List<ProtocolMetaData> result = new ArrayList<>(source.size()); for (ProtocolConnectionData pcd : source) { result.add(new ProtocolMetaData( pcd.getAccessPointId(), new ProtocolVersionPair(pcd.getProtocolId(), pcd.getProtocolVersion()), ByteBuffer.wrap(pcd.getConnectionData()))); } return result; }
/** * All-args constructor. */ public IpTransportInfo(TransportConnectionInfo parent) { super(parent.getServerType(), new ProtocolMetaData(parent.getAccessPointId(), new ProtocolVersionPair(parent.getTransportId() .getProtocolId(), parent.getTransportId().getProtocolVersion()), ByteBuffer.wrap(parent.getConnectionInfo()))); ByteBuffer buf = md.getConnectionInfo().duplicate(); byte[] publicKeyData = new byte[buf.getInt()]; buf.get(publicKeyData); try { this.publicKey = KeyUtil.getPublic(publicKeyData); } catch (InvalidKeyException ex) { LOG.error("Can't initialize public key", ex); throw new RuntimeException(ex); } byte[] hostData = new byte[buf.getInt()]; buf.get(hostData); this.host = new String(hostData, UTF8); this.port = buf.getInt(); }
/** * All-args constructor. */ public GenericTransportInfo(ServerType serverType, ProtocolMetaData md) { super(); this.serverType = serverType; this.md = md; this.transportId = new TransportProtocolId(md.getProtocolVersionInfo().getId(), md.getProtocolVersionInfo().getVersion()); }
@Override public byte[] getConnectionInfo() { return md.getConnectionInfo().array(); }
@Override public int hashCode() { int result = serverType != null ? serverType.hashCode() : 0; result = 31 * result + (transportId != null ? transportId.hashCode() : 0); result = 31 * result + (md != null ? md.hashCode() : 0); return result; }
public static ProtocolMetaData buildMetaData(TransportProtocolId id, String host, int port, PublicKey key) { ByteBuffer buf = ByteBuffer.wrap(new byte[SIZE_OF_INT * 3 + host.getBytes(UTF8).length + key.getEncoded().length]); buf.putInt(key.getEncoded().length); buf.put(key.getEncoded()); buf.putInt(host.getBytes(UTF8).length); buf.put(host.getBytes(UTF8)); buf.putInt(port); ProtocolMetaData md = new ProtocolMetaData((host + ":" + port).hashCode(), new ProtocolVersionPair(id.getProtocolId(), id.getProtocolVersion()), buf); return md; }
server.getProtocolVersionInfo().getId(), server.getProtocolVersionInfo().getVersion()); List<ProtocolMetaData> servers = mappedOperationServerList.get(transportId); if (servers == null) {
protected Map<TransportProtocolId, List<TransportConnectionInfo>> buildDummyConnectionInfo() { Map<TransportProtocolId, List<TransportConnectionInfo>> connectionInfo = new HashMap<>(); List<TransportConnectionInfo> connectionInfoList = new ArrayList<TransportConnectionInfo>(); connectionInfoList .add(new GenericTransportInfo(ServerType.BOOTSTRAP, new ProtocolMetaData(1, new ProtocolVersionPair(1, 1), null))); connectionInfo.put(new TransportProtocolId(1, 1), connectionInfoList); return connectionInfo; }
private List<ProtocolMetaData> getTransportsByAccessPointId(int accessPointId) { if (operationsServerList == null || operationsServerList.isEmpty()) { throw new BootstrapRuntimeException("Operations Server list is empty"); } List<ProtocolMetaData> result = new ArrayList<>(); for (ProtocolMetaData transport : operationsServerList) { if (transport.getAccessPointId().intValue() == accessPointId) { result.add(transport); } } return result; }
@Override public ProtocolMetaData build() { try { ProtocolMetaData record = new ProtocolMetaData(); record.accessPointId = fieldSetFlags()[0] ? this.accessPointId : (java.lang.Integer) defaultValue(fields()[0]); record.protocolVersionInfo = fieldSetFlags()[1] ? this.protocolVersionInfo : (org.kaaproject.kaa.common.endpoint.gen.ProtocolVersionPair) defaultValue(fields()[1]); record.connectionInfo = fieldSetFlags()[2] ? this.connectionInfo : (java.nio.ByteBuffer) defaultValue(fields()[2]); return record; } catch (Exception e) { throw new org.apache.avro.AvroRuntimeException(e); } } }
@Override public void useNextOperationsServer(TransportProtocolId transportId, FailoverStatus status) { if (mappedOperationServerList != null && !mappedOperationServerList.isEmpty()) { if (mappedIterators.get(transportId).hasNext()) { ProtocolMetaData nextOperationsServer = mappedIterators.get(transportId).next(); LOG.debug("New server [{}] will be user for [{}]", nextOperationsServer.getAccessPointId(), transportId); if (channelManager != null) { channelManager.onTransportConnectionInfoUpdated( new GenericTransportInfo(ServerType.OPERATIONS, nextOperationsServer)); } else { LOG.error("Can not process server change. Channel manager was not specified"); } } else { LOG.warn("Failed to find server for channel [{}]", transportId); resolveFailoverStatus(status); } } else { throw new BootstrapRuntimeException("Operations Server list is empty"); } }
@Test public void failureListenerTest() throws IOException, GeneralSecurityException { client.setFailoverStrategy(new DefaultFailoverStrategy() { @Override public FailoverDecision onFailover(FailoverStatus failoverStatus) { return new FailoverDecision(FailoverDecision.FailoverAction.FAILURE); } }); client.start(); ProtocolMetaData metaData = new ProtocolMetaData(1, new ProtocolVersionPair(1, 1), null); TransportConnectionInfo info = new GenericTransportInfo(ServerType.BOOTSTRAP, metaData); client.getChannelManager().onServerFailed(info, FailoverStatus.BOOTSTRAP_SERVERS_NA); Mockito.verify(stateListener, Mockito.timeout(500)).onStopped(); } }