public AwsDeployment(AwsDeploymentConfig config, int port) throws Exception { this.config = config; objectMapper = new ObjectMapper(); EnsembleProvider ensembleProvider = buildEnsembleProvider(config.getExhibitorConfig()); this.curatorClient = buildCuratorClient(ensembleProvider, config.getZookeeperConfig()); AwsIdentity identity = AwsIdentity.getIdentity(config); DeploymentPayload payload = new DeploymentPayload(identity.privateIp, port, identity.availabilityZone); this.groupMember = buildGroupMember(curatorClient, config.getZookeeperConfig(), identity.instanceId, payload); }
public AwsDeploymentConfig(Config config) { this.exhibitorConfig = new ExhibitorConfig(config.getConfig("exhibitor")); this.ipUrl = config.getString("ipUrl"); this.identityUrl = config.getString("identityUrl"); this.zookeeperConfig = new ZookeeperConfig(config.getConfig("zookeeper")); } }
private RetryPolicy buildRetryPolicy(RetryConfig config) { return new ExponentialBackoffRetry(config.getBaseSleepTimeMs(), config.getMaxRetries()); }
private EnsembleProvider buildEnsembleProvider(ExhibitorConfig config) throws Exception { Exhibitors exhibitors = new Exhibitors(config.getHostnames(), config.getRestPort(), () -> ""); RetryPolicy retryPolicy = buildRetryPolicy(config.getRetryConfig()); ExhibitorEnsembleProvider ensembleProvider = new ExhibitorEnsembleProvider( exhibitors, new DefaultExhibitorRestClient(), config.getRestUriPath(), config.getPollingMs(), retryPolicy); ensembleProvider.pollForInitialEnsemble(); return ensembleProvider; }
@Before public void beforeEach() throws Exception { zookeeper = new TestingServer(true); exhibitor = buildExhibitor(zookeeper.getPort()); exhibitor.start(); metaDataService = buildMetaDataService(); metaDataService.start(); Config config = ConfigFactory.load("aws-deployment-test") .withValue( "awsDeploymentTest.exhibitor.url", ConfigValueFactory.fromAnyRef(exhibitor.url("/").toString())) .withValue( "awsDeploymentTest.identityUrl", ConfigValueFactory.fromAnyRef(metaDataService.url("/").toString())); awsDeploymentConfig = new AwsDeploymentConfig(config.getConfig("awsDeploymentTest")); awsDeployment = new AwsDeployment(awsDeploymentConfig, 443); }
@Test public void testDeployment() throws Exception { awsDeployment.start(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeper.getConnectString(), retryPolicy)) { client.start(); client.blockUntilConnected(); String path = "/test-path/i-1234567890abcdef0"; Thread.sleep(2000); assertNotNull(client.checkExists().forPath(path)); String data = new String(client.getData().forPath(path)); ObjectMapper objectMapper = new ObjectMapper(); DeploymentPayload payload = objectMapper.readValue(data, new TypeReference<DeploymentPayload>() {}); assertEquals("10.158.112.84", payload.getHost()); assertEquals(443, payload.getPort()); assertEquals("us-west-2b", payload.getAvailabilityZone()); } } }
private CuratorFramework buildCuratorClient( EnsembleProvider ensembleProvider, ZookeeperConfig config) throws Exception { RetryPolicy retryPolicy = buildRetryPolicy(config.getRetryConfig()); CuratorFramework curatorClient = CuratorFrameworkFactory.builder() .ensembleProvider(ensembleProvider) .retryPolicy(retryPolicy) .build(); return curatorClient; }
@Test public void testCache() throws Exception { deploymentCache.start(); GroupMember groupMember1 = null; GroupMember groupMember2 = null; assertEquals(2, deploymentCache.getDeployments().size()); } finally { groupMember1.close();
public ZookeeperConfig(Config config) { this.membershipPath = config.getString("membershipPath"); this.retryConfig = new RetryConfig(config.getConfig("retry")); } }
private GroupMember buildGroupMember( CuratorFramework curatorClient, ZookeeperConfig config, String instanceId, DeploymentPayload payload) throws JsonProcessingException { byte[] payloadBytes = objectMapper.writeValueAsString(payload).getBytes(StandardCharsets.UTF_8); return new GroupMember(curatorClient, config.getMembershipPath(), instanceId, payloadBytes); }
public static AwsIdentity getIdentity(AwsDeploymentConfig config) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(config.getIdentityUrl()).build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readValue(response.body().string(), new TypeReference<AwsIdentity>() {}); } } }
@After public void afterEach() throws Exception { deploymentCache.close(); cacheClient.close(); zookeeper.close(); }
@After public void afterEach() throws Exception { awsDeployment.close(); zookeeper.close(); exhibitor.close(); metaDataService.close(); }
@Before public void beforeEach() throws Exception { zookeeper = new TestingServer(true); RetryPolicy retryPolicy = new RetryOneTime(1); cacheClient = CuratorFrameworkFactory.newClient(zookeeper.getConnectString(), retryPolicy); cacheClient.start(); cacheClient.blockUntilConnected(); deploymentCache = new DeploymentCache(cacheClient, "/member"); }
public ExhibitorConfig(Config config) { this.url = HttpUrl.parse(config.getString("url")); this.pollingMs = (int) config.getDuration("polling").toMillis(); this.retryConfig = new RetryConfig(config.getConfig("retry")); }