Example usage for java.util.concurrent.atomic AtomicReference AtomicReference

List of usage examples for java.util.concurrent.atomic AtomicReference AtomicReference

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicReference AtomicReference.

Prototype

public AtomicReference() 

Source Link

Document

Creates a new AtomicReference with null initial value.

Usage

From source file:com.microsoft.tfs.core.clients.versioncontrol.localworkspace.LocalWorkspaceScanner.java

private void diffItem(final EnumeratedLocalItem fromDisk, final WorkspaceLocalItem lvEntry) {
    if (fromDisk.isDirectory()) {
        if (!lvEntry.isDirectory()) {
            // Item is a directory on disk, but a file in the local version
            // table. Delete the local version row.
            markForRemoval.add(lvEntry);
        } else {//from w  w w. ja v a 2  s . c o m
            if (lvEntry.isMissingOnDisk()) {
                reappearedOnDisk.add(lvEntry);
            }
        }
    } else {
        if (lvEntry.isDirectory()) {
            // Item is a file on disk, but a directory in the local version
            // table. Delete the local version row.
            markForRemoval.add(lvEntry);
        } else {
            if (lvEntry.isMissingOnDisk()) {
                reappearedOnDisk.add(lvEntry);
            }

            boolean pendEdit = false;
            boolean symlink = false;

            if (lvEntry.isSymbolicLink() || fromDisk.isSymbolicLink()) {
                symlink = true;
            }

            if (-1 == lvEntry.getLength() || 0 == lvEntry.getHashValue().length) {
                // The local version row does not contain the data we need
                // to compare against.
                pendEdit = false;
            } else if (lvEntry.getLength() != fromDisk.getFileSize() && !symlink) {
                // File size has changed. This is a pending edit.
                pendEdit = true;
            } else {
                final long onDiskModifiedTime = fromDisk.getLastWriteTime();

                // 1. check content if modify time changed for normal file
                // 2. check whether link target has changed for symlink
                if (symlink || lvEntry.getLastModifiedTime() != onDiskModifiedTime) {
                    // Last modified date has changed. Hash the file to see
                    // if it has changed
                    pendEdit = true;

                    // If MD5 is a banned algorithm then the array will come
                    // back zero-length
                    byte[] onDiskHash = new byte[0];
                    try {
                        onDiskHash = CheckinEngine.computeMD5Hash(lvEntry.getLocalItem(), null);
                    } catch (final CoreCancelException e) {
                        // Won't happen because we passed a null TaskMonitor
                    }

                    if (onDiskHash.length > 0 && Arrays.equals(onDiskHash, lvEntry.getHashValue())) {
                        pendEdit = false;

                        // We will update the local version row to reflect
                        // the new last-modified time.
                        // Additionally, if the item has a pending edit, we
                        // will selective undo that pending edit.

                        toUndo.add(new KeyValuePair<String, Long>(lvEntry.getLocalItem(), onDiskModifiedTime));
                    }
                }
            }

            if (pendEdit && !skippedItems.contains(lvEntry.getLocalItem())) {
                final LocalPendingChange pcEntry = pc.getByLocalVersion(lvEntry);

                if (null == pcEntry || !pcEntry.isEdit()) {
                    final ChangeRequest changeRequest = new ChangeRequest(
                            new ItemSpec(lvEntry.getLocalItem(), RecursionType.NONE),
                            LatestVersionSpec.INSTANCE, RequestType.EDIT, ItemType.FILE,
                            VersionControlConstants.ENCODING_UNCHANGED, LockLevel.UNCHANGED, 0, null, false);

                    final AtomicReference<Failure[]> outDummy = new AtomicReference<Failure[]>();
                    final ChangeRequest[] changeRequests = new ChangeRequest[1];
                    changeRequests[0] = changeRequest;

                    LocalDataAccessLayer.pendEdit(LocalWorkspaceTransaction.getCurrent().getWorkspace(), wp, lv,
                            pc, changeRequests, true, outDummy, null);

                    // Since we've modified the pending changes table in a
                    // silent way, we want to set the flag on the
                    // transaction we're a part of that indicates the
                    // PendingChangesChanged event should be raised for this
                    // workspace, once the transaction completes.
                    LocalWorkspaceTransaction.getCurrent().setRaisePendingChangesChanged(true);
                }
            }

            /*
             * TEE-specific code to detect Unix symbolic links and execute
             * bit.
             */
            if (Platform.isCurrentPlatform(Platform.GENERIC_UNIX)
                    && LocalWorkspaceTransaction.getCurrent().getWorkspace().getClient().getServiceLevel()
                            .getValue() >= WebServiceLevel.TFS_2012.getValue()) {
                PropertyValue pendProperty = null;
                if (PlatformMiscUtils.getInstance()
                        .getEnvironmentVariable(EnvironmentVariables.DISABLE_SYMBOLIC_LINK_PROP) == null) {

                    final boolean isSymlink = PropertyConstants.IS_SYMLINK.equals(PropertyUtils
                            .selectMatching(lvEntry.getPropertyValues(), PropertyConstants.SYMBOLIC_KEY));

                    if (isSymlink != fromDisk.isSymbolicLink()) {
                        pendProperty = fromDisk.isSymbolicLink() ? PropertyConstants.IS_SYMLINK
                                : PropertyConstants.NOT_SYMLINK;
                    }

                    if (pendProperty != null && !skippedItems.contains(lvEntry.getLocalItem())) {
                        final ChangeRequest changeRequest = new ChangeRequest(
                                new ItemSpec(lvEntry.getLocalItem(), RecursionType.NONE),
                                LatestVersionSpec.INSTANCE, RequestType.PROPERTY, ItemType.FILE,
                                VersionControlConstants.ENCODING_UNCHANGED, LockLevel.UNCHANGED, 0, null,
                                false);

                        changeRequest.setProperties(new PropertyValue[] { pendProperty });

                        final AtomicBoolean outOnlineOperationRequired = new AtomicBoolean();
                        final AtomicReference<Failure[]> outDummy = new AtomicReference<Failure[]>();
                        final ChangeRequest[] changeRequests = new ChangeRequest[1];
                        changeRequests[0] = changeRequest;

                        // Include property filters so any listeners have
                        // them
                        LocalDataAccessLayer.pendPropertyChange(
                                LocalWorkspaceTransaction.getCurrent().getWorkspace(), wp, lv, pc,
                                changeRequests, true, outDummy, outOnlineOperationRequired,
                                new String[] { PropertyConstants.SYMBOLIC_KEY });

                        LocalWorkspaceTransaction.getCurrent().setRaisePendingChangesChanged(true);
                    }
                }

                if (pendProperty == null && PlatformMiscUtils.getInstance()
                        .getEnvironmentVariable(EnvironmentVariables.DISABLE_DETECT_EXECUTABLE_PROP) == null) {

                    final boolean lvExecutable = PropertyConstants.EXECUTABLE_ENABLED_VALUE.equals(PropertyUtils
                            .selectMatching(lvEntry.getPropertyValues(), PropertyConstants.EXECUTABLE_KEY));

                    if (lvExecutable != fromDisk.isExecutable()) {
                        pendProperty = fromDisk.isExecutable() ? PropertyConstants.EXECUTABLE_ENABLED_VALUE
                                : PropertyConstants.EXECUTABLE_DISABLED_VALUE;
                    }

                    if (pendProperty != null && !skippedItems.contains(lvEntry.getLocalItem())) {
                        final ChangeRequest changeRequest = new ChangeRequest(
                                new ItemSpec(lvEntry.getLocalItem(), RecursionType.NONE),
                                LatestVersionSpec.INSTANCE, RequestType.PROPERTY, ItemType.FILE,
                                VersionControlConstants.ENCODING_UNCHANGED, LockLevel.UNCHANGED, 0, null,
                                false);

                        changeRequest.setProperties(new PropertyValue[] { pendProperty });

                        final AtomicBoolean outOnlineOperationRequired = new AtomicBoolean();
                        final AtomicReference<Failure[]> outDummy = new AtomicReference<Failure[]>();
                        final ChangeRequest[] changeRequests = new ChangeRequest[1];
                        changeRequests[0] = changeRequest;

                        // Include property filters so any listeners have
                        // them
                        LocalDataAccessLayer.pendPropertyChange(
                                LocalWorkspaceTransaction.getCurrent().getWorkspace(), wp, lv, pc,
                                changeRequests, true, outDummy, outOnlineOperationRequired,
                                new String[] { PropertyConstants.EXECUTABLE_KEY });

                        /*
                         * Since we've modified the pending changes table in
                         * a silent way, we want to set the flag on the
                         * transaction we're a part of that indicates the
                         * PendingChangesChanged event should be raised for
                         * this workspace, once the transaction completes.
                         */
                        LocalWorkspaceTransaction.getCurrent().setRaisePendingChangesChanged(true);
                    }
                }
            }
        }
    }
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.UpdateLocalVersionQueue.java

/**
 * Sends the given updates to the server.
 *
 * @param updates//  w  w w  .  j  a v  a 2s  .c  o m
 *        the updates to send (must not be <code>null</code>)
 */
private IPopulatableLocalVersionUpdate[] sendToServer(final ILocalVersionUpdate[] updates) {
    Check.notNull(updates, "updates"); //$NON-NLS-1$

    log.debug(MessageFormat.format("Sending {0} updates to the server (options={1})", //$NON-NLS-1$
            Integer.toString(updates.length), options));

    if (updates.length == 0) {
        return null;
    }

    final AtomicReference<IPopulatableLocalVersionUpdate[]> updatesMissingBaselines = new AtomicReference<IPopulatableLocalVersionUpdate[]>();

    if (workspace.getLocation() == WorkspaceLocation.LOCAL
            && options.contains(UpdateLocalVersionQueueOptions.UPDATE_LOCAL)) {
        /*
         * We cannot perform baseline folder maintenance with the LV or PC
         * tables open. The baseline folder maintenance could cause them to
         * move.
         */
        final LocalWorkspaceTransaction baselineMaintTransaction = new LocalWorkspaceTransaction(workspace,
                wLock);
        try {
            baselineMaintTransaction.execute(new WorkspacePropertiesTransaction() {
                @Override
                public void invoke(final LocalWorkspaceProperties wp) {
                    wp.doBaselineFolderMaintenance();
                }
            });
        } finally {
            try {
                baselineMaintTransaction.close();
            } catch (final IOException e) {
                throw new VersionControlException(e);
            }
        }

        /*
         * Must run while synchronized(this) because
         * persistedDisplacedBaselines is accessed.
         */
        synchronized (this) {
            final LocalWorkspaceTransaction updateTransaction = new LocalWorkspaceTransaction(workspace, wLock);
            try {
                updateTransaction.execute(new AllTablesTransaction() {
                    @Override
                    public void invoke(final LocalWorkspaceProperties wp, final WorkspaceVersionTable lv,
                            final LocalPendingChangesTable pc) {
                        if (options.contains(UpdateLocalVersionQueueOptions.UPDATE_SERVER)) {
                            ILocalVersionUpdate[] acks;

                            synchronized (pendingAcks) {
                                acks = pendingAcks.toArray(new ILocalVersionUpdate[pendingAcks.size()]);
                                pendingAcks.clear();
                            }

                            if (acks.length > 0) {
                                LocalDataAccessLayer.acknowledgeUpdateLocalVersion(lv, acks);
                            }
                        }

                        LocalDataAccessLayer.updateLocalVersion(workspace, wp, lv, pc, updates,
                                persistedDisplacedBaselines, updatesMissingBaselines);
                    }
                });
            } finally {
                try {
                    updateTransaction.close();
                } catch (final IOException e) {
                    throw new VersionControlException(e);
                }
            }
        }
    }

    if (options.contains(UpdateLocalVersionQueueOptions.UPDATE_SERVER)) {
        workspace.getClient().getWebServiceLayer().updateLocalVersion(workspace.getName(),
                workspace.getOwnerName(), updates);

        if (options.contains(UpdateLocalVersionQueueOptions.UPDATE_LOCAL)
                && WorkspaceLocation.LOCAL == workspace.getLocation()) {
            synchronized (pendingAcks) {
                for (final ILocalVersionUpdate update : updates) {
                    pendingAcks.add(update);
                }
            }
        }
    }

    return updatesMissingBaselines.get();
}

From source file:io.atomix.protocols.gossip.map.AntiEntropyMapDelegate.java

private void processUpdates(Collection<UpdateEntry> updates) {
    if (closed) {
        return;//  www .  j  a v  a 2  s . c om
    }
    updates.forEach(update -> {
        final String key = update.key();
        final MapValue value = update.value() == null ? null : update.value().copy();
        if (value == null || value.isTombstone()) {
            MapValue previousValue = removeInternal(key, Optional.empty(), Optional.ofNullable(value));
            if (previousValue != null && previousValue.isAlive()) {
                notifyListeners(
                        new MapDelegateEvent<>(REMOVE, decodeKey(key), previousValue.get(this::decodeValue)));
            }
        } else {
            counter.incrementCount();
            AtomicReference<byte[]> oldValue = new AtomicReference<>();
            AtomicBoolean updated = new AtomicBoolean(false);
            items.compute(key, (k, existing) -> {
                if (existing == null || value.isNewerThan(existing)) {
                    updated.set(true);
                    oldValue.set(existing != null ? existing.get() : null);
                    return value;
                }
                return existing;
            });

            if (updated.get()) {
                if (oldValue.get() == null) {
                    notifyListeners(new MapDelegateEvent<>(INSERT, decodeKey(key), decodeValue(value.get())));
                } else {
                    notifyListeners(new MapDelegateEvent<>(UPDATE, decodeKey(key), decodeValue(value.get())));
                }
            }
        }
    });
}

From source file:de.schildbach.pte.AbstractEfaProvider.java

protected NearbyLocationsResult mobileCoordRequest(final EnumSet<LocationType> types, final int lat,
        final int lon, final int maxDistance, final int maxStations) throws IOException {
    final HttpUrl.Builder url = coordEndpoint.newBuilder();
    appendXmlCoordRequestParameters(url, types, lat, lon, maxDistance, maxStations);
    final AtomicReference<NearbyLocationsResult> result = new AtomicReference<>();

    final HttpClient.Callback callback = new HttpClient.Callback() {
        @Override/*from ww w. j  av a  2  s  .c o  m*/
        public void onSuccessful(final CharSequence bodyPeek, final ResponseBody body) throws IOException {
            try {
                final XmlPullParser pp = parserFactory.newPullParser();
                pp.setInput(body.byteStream(), null); // Read encoding from XML declaration
                final ResultHeader header = enterEfa(pp);

                XmlPullUtil.enter(pp, "ci");

                XmlPullUtil.enter(pp, "request");
                XmlPullUtil.skipExit(pp, "request");

                final List<Location> stations = new ArrayList<>();

                if (XmlPullUtil.optEnter(pp, "pis")) {
                    while (XmlPullUtil.optEnter(pp, "pi")) {
                        final String name = normalizeLocationName(XmlPullUtil.optValueTag(pp, "de", null));
                        final String type = XmlPullUtil.valueTag(pp, "ty");
                        final LocationType locationType;
                        if ("STOP".equals(type))
                            locationType = LocationType.STATION;
                        else if ("POI_POINT".equals(type))
                            locationType = LocationType.POI;
                        else
                            throw new IllegalStateException("unknown type: " + type);

                        final String id = XmlPullUtil.valueTag(pp, "id");
                        XmlPullUtil.valueTag(pp, "omc");
                        XmlPullUtil.optValueTag(pp, "pid", null);
                        final String place = normalizeLocationName(XmlPullUtil.valueTag(pp, "locality"));
                        XmlPullUtil.valueTag(pp, "layer");
                        XmlPullUtil.valueTag(pp, "gisID");
                        XmlPullUtil.valueTag(pp, "ds");
                        XmlPullUtil.valueTag(pp, "stateless");
                        final Point coord = parseCoord(XmlPullUtil.valueTag(pp, "c"));

                        final Location location;
                        if (name != null)
                            location = new Location(locationType, id, coord, place, name);
                        else
                            location = new Location(locationType, id, coord, null, place);
                        stations.add(location);

                        XmlPullUtil.skipExit(pp, "pi");
                    }

                    XmlPullUtil.skipExit(pp, "pis");
                }

                XmlPullUtil.skipExit(pp, "ci");

                result.set(new NearbyLocationsResult(header, stations));
            } catch (final XmlPullParserException x) {
                throw new ParserException("cannot parse xml: " + bodyPeek, x);
            }
        }
    };

    if (httpPost)
        httpClient.getInputStream(callback, url.build(), url.build().encodedQuery(),
                "application/x-www-form-urlencoded", httpReferer);
    else
        httpClient.getInputStream(callback, url.build(), httpReferer);

    return result.get();
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.localworkspace.BaselineFolderCollection.java

public void processBaselineRequests(final Workspace workspace, final Iterable<BaselineRequest> requests) {
    final AtomicReference<Iterable<BaselineRequest>> outFailedLocalRequests = new AtomicReference<Iterable<BaselineRequest>>();

    try {/*w  w w. j ava 2s.  co m*/
        processBaselineRequests(workspace, requests, false, outFailedLocalRequests);
    } catch (final CoreCancelException e) {
        // Can't happen because we're passing false for throwIfCanceled.
    }
}

From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java

@Test
public void testData() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> failure = new AtomicReference<>();

    URI uri = new URI("ws://localhost:" + port + "/composite");
    WebSocketStompClient stompClient = new WebSocketStompClient(uri, this.headers, sockJsClient);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());

    stompClient.connect(new StompMessageHandler() {

        private StompSession stompSession;

        @Override/*from  w ww  .j  a  va 2 s  . co  m*/
        public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) {
            this.stompSession = stompSession;
            String topicUuid = simulateJoinEvent();

            this.stompSession.subscribe("/user/queue/device", null);
            this.stompSession.subscribe(String.format("/topic/%s", topicUuid), null);

            try {
                // send a simple hashmap to the data endpoint
                HashMap<String, Object> data = new HashMap<String, Object>();
                data.put("data", "TEST-DATA Data");
                data.put("type", "data");
                this.stompSession.send(String.format("/app/%s", topicUuid), data);
            } catch (Throwable t) {
                failure.set(t);
                latch.countDown();
            }
        }

        @Override
        public void handleMessage(Message<byte[]> message) {
            try {
                String json = parseMessageJson(message);
                new JsonPathExpectationsHelper("type").exists(json);
                new JsonPathExpectationsHelper("type").assertValue(json, "data");
                new JsonPathExpectationsHelper("serverTime").exists(json);
                new JsonPathExpectationsHelper("data").assertValue(json, "TEST-DATA Data");
            } catch (Throwable t) {
                failure.set(t);
            } finally {
                this.stompSession.disconnect();
                latch.countDown();
            }
        }

        @Override
        public void handleError(Message<byte[]> message) {
            StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
            String error = "[Producer] " + accessor.getShortLogMessage(message.getPayload());
            logger.error(error);
            failure.set(new Exception(error));
        }

        @Override
        public void handleReceipt(String receiptId) {
        }

        @Override
        public void afterDisconnected() {
        }

    });

    if (!latch.await(10, TimeUnit.SECONDS)) {
        fail("Data response not received");
    } else if (failure.get() != null) {
        throw new AssertionError("", failure.get());
    }
}

From source file:com.clxcommunications.xms.ApiConnectionIT.java

@Test
public void canHandle500WhenFetchingBatch() throws Exception {
    String spid = TestUtils.freshServicePlanId();
    BatchId batchId = TestUtils.freshBatchId();

    String path = "/v1/" + spid + "/batches/" + batchId;

    wm.stubFor(get(urlEqualTo(path)).willReturn(aResponse().withStatus(500)
            .withHeader("Content-Type", ContentType.TEXT_PLAIN.toString()).withBody("BAD")));

    ApiConnection conn = ApiConnection.builder().servicePlanId(spid).token("tok")
            .endpoint("http://localhost:" + wm.port()).start();

    /*//from  w  w  w.j a  v a  2s .c o  m
     * The exception we'll receive in the callback. Need to store it to
     * verify that it is the same exception as received from #get().
     */
    final AtomicReference<Exception> failException = new AtomicReference<Exception>();

    try {
        /*
         * Used to make sure callback and test thread are agreeing about the
         * failException variable.
         */
        final CountDownLatch latch = new CountDownLatch(1);

        FutureCallback<MtBatchSmsResult> testCallback = new TestCallback<MtBatchSmsResult>() {

            @Override
            public void failed(Exception exception) {
                if (!failException.compareAndSet(null, exception)) {
                    fail("failed called multiple times");
                }

                latch.countDown();
            }

        };

        Future<MtBatchSmsResult> future = conn.fetchBatchAsync(batchId, testCallback);

        // Give plenty of time for the callback to be called.
        latch.await();

        future.get();
        fail("unexpected future get success");
    } catch (ExecutionException ee) {
        /*
         * The exception cause should be the same as we received in the
         * callback.
         */
        assertThat(failException.get(), is(theInstance(ee.getCause())));
        assertThat(ee.getCause(), is(instanceOf(UnexpectedResponseException.class)));

        UnexpectedResponseException ure = (UnexpectedResponseException) ee.getCause();

        HttpResponse response = ure.getResponse();
        assertThat(response, notNullValue());
        assertThat(response.getStatusLine().getStatusCode(), is(500));
        assertThat(response.getEntity().getContentType().getValue(), is(ContentType.TEXT_PLAIN.toString()));

        byte[] buf = new byte[100];
        int read;

        InputStream contentStream = null;
        try {
            contentStream = response.getEntity().getContent();
            read = contentStream.read(buf);
        } catch (IOException ioe) {
            throw new AssertionError("unexpected exception: " + ioe.getMessage(), ioe);
        } finally {
            if (contentStream != null) {
                try {
                    contentStream.close();
                } catch (IOException ioe) {
                    throw new AssertionError("unexpected exception: " + ioe.getMessage(), ioe);
                }
            }
        }

        assertThat(read, is(3));
        assertThat(Arrays.copyOf(buf, 3), is(new byte[] { 'B', 'A', 'D' }));
    } finally {
        conn.close();
    }

    verifyGetRequest(path);
}

From source file:com.jivesoftware.os.upena.deployable.UpenaMain.java

public void run(String[] args) throws Exception {

    HealthFactory.initialize(BindInterfaceToConfiguration::bindDefault, new HealthCheckRegistry() {

        @Override//from   ww w  .  jav  a2s  .c om
        public void register(HealthChecker<?> healthChecker) {

        }

        @Override
        public void unregister(HealthChecker<?> healthChecker) {

        }
    });

    Properties buildProperties = new Properties();
    String upenaVersion = "";
    try {
        buildProperties.load(UpenaMain.class.getClassLoader().getResourceAsStream("build.properties"));
        upenaVersion = buildProperties.getProperty("my.version", "") + " "
                + buildProperties.getProperty("my.timestamp", "") + " sha:"
                + buildProperties.getProperty("git.commit.id", "");
    } catch (Exception x) {
        LOG.warn("Failed to locate build.properties");
    }

    String workingDir = System.getProperty("user.dir");
    long start = System.currentTimeMillis();
    Exception failed = null;
    while (start + TimeUnit.SECONDS.toMillis(10) > System.currentTimeMillis()) {
        try {
            File lockFile = new File(workingDir, "onlyLetOneRunningAtATime");
            lockFile.createNewFile();
            FileChannel.open(lockFile.toPath(), StandardOpenOption.WRITE).lock();
            failed = null;
            break;
        } catch (Exception x) {
            failed = x;
            LOG.warn("Failed to acquire lock on onlyLetOneRunningAtATime", x);
            Thread.sleep(1000);
        }
    }
    if (failed != null) {
        throw failed;
    }

    JDIAPI jvmapi = null;
    try {
        jvmapi = new JDIAPI();
    } catch (NoClassDefFoundError x) {
        LOG.warn(
                "Failed to local tools.jar. Please manually add to classpath. Breakpoint debugger will be disabled.");
    }

    String hostname = args[0];

    int loopbackPort = Integer.parseInt(System.getProperty("amza.loopback.port", "1174"));
    int port = Integer.parseInt(System.getProperty("amza.port", "1175"));
    String multicastGroup = System.getProperty("amza.discovery.group", "225.4.5.6");
    int multicastPort = Integer.parseInt(System.getProperty("amza.discovery.port", "1123"));
    String clusterDiscoveryName = (args.length > 1 ? args[1] : null);

    String datacenter = System.getProperty("host.datacenter", "unknownDatacenter");
    String rack = System.getProperty("host.rack", "unknownRack");
    String publicHost = System.getProperty("public.host.name", hostname);

    UpenaRingHost ringHost = new UpenaRingHost(hostname, port); // TODO include rackId

    // todo need a better way to create writer id.
    int writerId = new Random().nextInt(512);
    TimestampedOrderIdProvider orderIdProvider = new OrderIdProviderImpl(
            new ConstantWriterIdProvider(writerId));

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    RowsStorageProvider rowsStorageProvider = rowsStorageProvider(orderIdProvider);

    boolean sslEnable = Boolean.parseBoolean(System.getProperty("ssl.enabled", "true"));
    String sslKeystorePassword = System.getProperty("ssl.keystore.password", "password");
    String sslKeystorePath = System.getProperty("ssl.keystore.path", "./certs/sslKeystore");
    String sslKeyStoreAlias = System.getProperty("ssl.keystore.alias", "upenanode").toLowerCase();
    boolean sslAutoGenerateSelfSignedCert = Boolean
            .parseBoolean(System.getProperty("ssl.keystore.autoGenerate", "true"));

    File sslKeystore = new File(sslKeystorePath);
    if (sslEnable) {
        SelfSigningCertGenerator selfSigningCertGenerator = new SelfSigningCertGenerator();
        if (sslKeystore.exists()) {
            if (!selfSigningCertGenerator.validate(sslKeyStoreAlias, sslKeystorePassword, sslKeystore)) {
                LOG.error("SSL keystore validation failed. keyStoreAlias:{} sslKeystore:{}", sslKeyStoreAlias,
                        sslKeystore);
                System.exit(1);
            }
        } else {
            sslKeystore.getParentFile().mkdirs();
            if (sslAutoGenerateSelfSignedCert) {
                selfSigningCertGenerator.create(sslKeyStoreAlias, sslKeystorePassword, sslKeystore);
            } else {
                LOG.error("Failed to locate mandatory sslKeystore:{}", sslKeystore);
                System.exit(1);
            }
        }
    }

    String consumerKey = System.getProperty("upena.consumerKey", clusterDiscoveryName);
    if (consumerKey == null) {
        consumerKey = "upena";
        LOG.warn("Please provide a stronger consumerKey via -Dupena.consumerKey");
    }
    String finalConsumerKey = consumerKey;

    String secret = System.getProperty("upena.secret");
    if (secret == null) {
        secret = "secret";
        LOG.warn("Please provide a stronger secret via -Dupena.secret");
    }
    String finalSecret = secret;

    OAuthSigner authSigner = (request) -> {
        CommonsHttpOAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(finalConsumerKey, finalSecret);
        oAuthConsumer.setMessageSigner(new HmacSha1MessageSigner());
        oAuthConsumer.setTokenWithSecret(finalConsumerKey, finalSecret);
        return oAuthConsumer.sign(request);
    };
    UpenaSSLConfig upenaSSLConfig = new UpenaSSLConfig(sslEnable, sslAutoGenerateSelfSignedCert, authSigner);

    UpenaAmzaService upenaAmzaService = null;
    if (!new File("./state").exists()) {
        UpdatesSender changeSetSender = new HttpUpdatesSender(sslEnable, sslAutoGenerateSelfSignedCert,
                authSigner);
        UpdatesTaker tableTaker = new HttpUpdatesTaker(sslEnable, sslAutoGenerateSelfSignedCert, authSigner);

        UpenaAmzaServiceConfig upenaAmzaServiceConfig = new UpenaAmzaServiceConfig();
        upenaAmzaService = new UpenaAmzaServiceInitializer().initialize(upenaAmzaServiceConfig, orderIdProvider,
                new com.jivesoftware.os.upena.amza.storage.FstMarshaller(
                        FSTConfiguration.getDefaultConfiguration()),
                rowsStorageProvider, rowsStorageProvider, rowsStorageProvider, changeSetSender, tableTaker,
                Optional.<SendFailureListener>absent(), Optional.<UpenaTakeFailureListener>absent(),
                (changes) -> {
                });

        /*upenaAmzaService.start(ringHost, upenaAmzaServiceConfig.resendReplicasIntervalInMillis,
            upenaAmzaServiceConfig.applyReplicasIntervalInMillis,
            upenaAmzaServiceConfig.takeFromNeighborsIntervalInMillis,
            upenaAmzaServiceConfig.checkIfCompactionIsNeededIntervalInMillis,
            upenaAmzaServiceConfig.compactTombstoneIfOlderThanNMillis);*/

        LOG.info("-----------------------------------------------------------------------");
        LOG.info("|      OLD Amza Service Online");
        LOG.info("-----------------------------------------------------------------------");
    } else {
        LOG.info("-----------------------------------------------------------------------");
        LOG.info("|      OLD Amza Data is decomissionable");
        LOG.info("-----------------------------------------------------------------------");
    }

    BAInterner baInterner = new BAInterner();

    AtomicReference<Callable<RingTopology>> topologyProvider = new AtomicReference<>(); // bit of a hack
    InstanceDescriptor instanceDescriptor = new InstanceDescriptor(datacenter, rack, "", "", "", "", "", "", "",
            "", 0, "", "", "", 0L, true);
    ConnectionDescriptorsProvider noAuthConnectionsProvider = (connectionDescriptorsRequest,
            expectedReleaseGroup) -> {
        try {
            RingTopology systemRing = topologyProvider.get().call();
            List<ConnectionDescriptor> descriptors = Lists.newArrayList(Iterables.transform(systemRing.entries,
                    input -> new ConnectionDescriptor(instanceDescriptor, sslEnable, false,
                            new HostPort(input.ringHost.getHost(), input.ringHost.getPort()),
                            Collections.emptyMap(), Collections.emptyMap())));
            return new ConnectionDescriptorsResponse(200, Collections.emptyList(), "", descriptors,
                    connectionDescriptorsRequest.getRequestUuid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };

    TenantsServiceConnectionDescriptorProvider<String> noAuthConnectionPoolProvider = new TenantsServiceConnectionDescriptorProvider<>(
            Executors.newScheduledThreadPool(1), "", noAuthConnectionsProvider, "", "", 10_000); // TODO config
    noAuthConnectionPoolProvider.start();

    ConnectionDescriptorsProvider connectionsProvider = (connectionDescriptorsRequest,
            expectedReleaseGroup) -> {
        try {
            RingTopology systemRing = topologyProvider.get().call();
            List<ConnectionDescriptor> descriptors = Lists.newArrayList(Iterables.transform(systemRing.entries,
                    input -> new ConnectionDescriptor(instanceDescriptor, sslEnable, true,
                            new HostPort(input.ringHost.getHost(), input.ringHost.getPort()),
                            Collections.emptyMap(), Collections.emptyMap())));
            return new ConnectionDescriptorsResponse(200, Collections.emptyList(), "", descriptors,
                    connectionDescriptorsRequest.getRequestUuid());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };

    TenantsServiceConnectionDescriptorProvider<String> connectionPoolProvider = new TenantsServiceConnectionDescriptorProvider<>(
            Executors.newScheduledThreadPool(1), "", connectionsProvider, "", "", 10_000); // TODO config
    connectionPoolProvider.start();

    HttpDeliveryClientHealthProvider clientHealthProvider = new HttpDeliveryClientHealthProvider("", null, "",
            5000, 100);

    TenantRoutingHttpClientInitializer<String> nonSigningClientInitializer = new TenantRoutingHttpClientInitializer<>(
            null);

    TenantAwareHttpClient<String> systemTakeClient = nonSigningClientInitializer
            .builder(noAuthConnectionPoolProvider, // TODO config
                    clientHealthProvider)
            .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).maxConnections(1_000)
            .socketTimeoutInMillis(60_000).build(); // TODO expose to conf
    TenantAwareHttpClient<String> stripedTakeClient = nonSigningClientInitializer
            .builder(noAuthConnectionPoolProvider, // TODO config
                    clientHealthProvider)
            .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).maxConnections(1_000)
            .socketTimeoutInMillis(60_000).build(); // TODO expose to conf

    TenantRoutingHttpClientInitializer<String> tenantRoutingHttpClientInitializer = new TenantRoutingHttpClientInitializer<>(
            new OAuthSignerProvider(() -> authSigner));

    TenantAwareHttpClient<String> ringClient = tenantRoutingHttpClientInitializer
            .builder(connectionPoolProvider, // TODO config
                    clientHealthProvider)
            .deadAfterNErrors(10).checkDeadEveryNMillis(10_000).maxConnections(1_000)
            .socketTimeoutInMillis(60_000).build(); // TODO expose to conf
    AmzaStats amzaStats = new AmzaStats();

    AmzaService amzaService = startAmza(workingDir, amzaStats, baInterner, writerId,
            new RingHost(datacenter, rack, ringHost.getHost(), ringHost.getPort()),
            new RingMember(ringHost.getHost() + ":" + ringHost.getPort()), authSigner, systemTakeClient,
            stripedTakeClient, ringClient, topologyProvider, clusterDiscoveryName, multicastGroup,
            multicastPort);

    EmbeddedClientProvider embeddedClientProvider = new EmbeddedClientProvider(amzaService);

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Amza Service Online");
    LOG.info("-----------------------------------------------------------------------");

    ObjectMapper storeMapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    UpenaConfigStore upenaConfigStore = new UpenaConfigStore(orderIdProvider, storeMapper, upenaAmzaService,
            amzaService, embeddedClientProvider);

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Upena Config Store Online");
    LOG.info("-----------------------------------------------------------------------");

    ExecutorService instanceChangedThreads = Executors.newFixedThreadPool(32);

    AtomicReference<UbaService> ubaServiceReference = new AtomicReference<>();
    UpenaStore upenaStore = new UpenaStore(storeMapper, upenaAmzaService, (instanceChanges) -> {
        instanceChangedThreads.submit(() -> {
            UbaService got = ubaServiceReference.get();
            if (got != null) {
                try {
                    got.instanceChanged(instanceChanges);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }, (changes) -> {
    }, (change) -> {
        LOG.info("TODO: tie into conductor. " + change);
    }, amzaService, embeddedClientProvider);
    //upenaStore.attachWatchers();

    ChaosService chaosService = new ChaosService(upenaStore);
    SecureRandom random = new SecureRandom();
    PasswordStore passwordStore = (key) -> {
        String password = System.getProperty("sauth.keystore.password");
        if (password == null) {
            File passwordFile = new File(workingDir, "keystore/" + key + ".key");
            if (passwordFile.exists()) {
                password = Files.toString(passwordFile, StandardCharsets.UTF_8);
            } else {
                passwordFile.getParentFile().mkdirs();
                password = new BigInteger(130, random).toString(32);
                Files.write(password, passwordFile, StandardCharsets.UTF_8);
            }
        }
        return password;
    };

    SessionStore sessionStore = new SessionStore(TimeUnit.MINUTES.toMillis(60), TimeUnit.MINUTES.toMillis(30));

    AtomicReference<UpenaHealth> upenaHealthProvider = new AtomicReference<>();
    InstanceHealthly instanceHealthly = (key, version) -> {
        UpenaHealth upenaHealth = upenaHealthProvider.get();
        if (upenaHealth == null) {
            return false;
        }
        ConcurrentMap<RingHost, NodeHealth> ringHostNodeHealth = upenaHealth.buildClusterHealth();
        for (NodeHealth nodeHealth : ringHostNodeHealth.values()) {
            for (NannyHealth nannyHealth : nodeHealth.nannyHealths) {
                if (nannyHealth.instanceDescriptor.instanceKey.equals(key.getKey())) {
                    return nannyHealth.serviceHealth.fullyOnline
                            ? nannyHealth.serviceHealth.version.equals(version)
                            : false;
                }
            }
        }
        return false;
    };
    UpenaService upenaService = new UpenaService(passwordStore, sessionStore, upenaStore, chaosService,
            instanceHealthly);

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Upena Service Online");
    LOG.info("-----------------------------------------------------------------------");

    File defaultPathToRepo = new File(new File(System.getProperty("user.dir"), ".m2"), "repository");
    PathToRepo localPathToRepo = new PathToRepo(
            new File(System.getProperty("pathToRepo", defaultPathToRepo.getAbsolutePath())));
    RepositoryProvider repositoryProvider = new RepositoryProvider(localPathToRepo);

    Host host = new Host(publicHost, datacenter, rack, ringHost.getHost(), ringHost.getPort(), workingDir, null,
            null);
    HostKey hostKey = new HostKeyProvider().getNodeKey(upenaStore.hosts, host);

    String hostInstanceId = System.getProperty("host.instance.id", hostKey.getKey());
    host = new Host(publicHost, datacenter, rack, ringHost.getHost(), ringHost.getPort(), workingDir,
            hostInstanceId, null);

    UbaLog ubaLog = (what, why, how) -> {
        try {
            upenaStore.record("Uba", what, System.currentTimeMillis(), why,
                    ringHost.getHost() + ":" + ringHost.getPort(), how);
        } catch (Exception x) {
            x.printStackTrace(); // Hmm lame
        }
    };

    OktaLog oktaLog = (who, what, why, how) -> {
        try {
            upenaStore.record("okta:" + who, what, System.currentTimeMillis(), why,
                    ringHost.getHost() + ":" + ringHost.getPort(), how);
        } catch (Exception x) {
            x.printStackTrace(); // Hmm lame
        }
    };

    OktaCredentialsMatcher.oktaLog = oktaLog;
    OktaRealm.oktaLog = oktaLog;

    UpenaClient upenaClient = new UpenaClient() {
        @Override
        public InstanceDescriptorsResponse instanceDescriptor(
                InstanceDescriptorsRequest instanceDescriptorsRequest) throws Exception {
            return upenaService.instanceDescriptors(instanceDescriptorsRequest);
        }

        @Override
        public void updateKeyPair(String instanceKey, String publicKey) throws Exception {
            Instance i = upenaStore.instances.get(new InstanceKey(instanceKey));
            if (i != null) {
                LOG.info("Updating publicKey for {}", instanceKey);
                upenaStore.instances.update(new InstanceKey(instanceKey),
                        new Instance(i.clusterKey, i.hostKey, i.serviceKey, i.releaseGroupKey, i.instanceId,
                                i.enabled, i.locked, publicKey, i.restartTimestampGMTMillis, i.ports));
            }
        }

    };

    final UbaService ubaService = new UbaServiceInitializer().initialize(passwordStore, upenaClient,
            repositoryProvider, hostKey.getKey(), workingDir,
            new UbaCoordinate(datacenter, rack, publicHost, host.hostName, "localhost", loopbackPort), null,
            ubaLog);

    UpenaHealth upenaHealth = new UpenaHealth(amzaService, upenaSSLConfig, upenaConfigStore, ubaService,
            new RingHost(datacenter, rack, ringHost.getHost(), ringHost.getPort()), hostKey);
    upenaHealthProvider.set(upenaHealth);

    DiscoveredRoutes discoveredRoutes = new DiscoveredRoutes();
    ShiroRequestHelper shiroRequestHelper = new ShiroRequestHelper(TimeUnit.DAYS.toMillis(1)); // TODO expose Sys prop?

    String shiroConfigLocation = System.getProperty("shiro.ini.location", "classpath:shiro.ini"); // classpath:oktashiro.ini

    UpenaJerseyEndpoints jerseyEndpoints = new UpenaJerseyEndpoints(shiroConfigLocation)
            .addInjectable(ShiroRequestHelper.class, shiroRequestHelper)
            .addEndpoint(UpenaClusterRestEndpoints.class).addEndpoint(UpenaHostRestEndpoints.class)
            .addEndpoint(UpenaServiceRestEndpoints.class).addEndpoint(UpenaReleaseRestEndpoints.class)
            .addEndpoint(UpenaInstanceRestEndpoints.class).addEndpoint(UpenaTenantRestEndpoints.class)
            .addInjectable(upenaHealth).addInjectable(upenaService).addInjectable(upenaStore)
            .addInjectable(upenaConfigStore).addInjectable(ubaService)
            //.addEndpoint(AmzaReplicationRestEndpoints.class)
            //.addInjectable(UpenaAmzaInstance.class, upenaAmzaService)
            .addEndpoint(UpenaEndpoints.class).addEndpoint(UpenaConnectivityEndpoints.class)
            .addEndpoint(UpenaManagedDeployableEndpoints.class).addEndpoint(UpenaHealthEndpoints.class)
            .addEndpoint(UpenaRepoEndpoints.class).addInjectable(DiscoveredRoutes.class, discoveredRoutes)
            .addInjectable(UpenaRingHost.class, ringHost).addInjectable(HostKey.class, hostKey)
            .addInjectable(UpenaAutoRelease.class, new UpenaAutoRelease(repositoryProvider, upenaStore))
            .addInjectable(PathToRepo.class, localPathToRepo);

    PercentileHealthCheckConfig phcc = bindDefault(PercentileHealthCheckConfig.class);
    PercentileHealthChecker authFilterHealthCheck = new PercentileHealthChecker(phcc);
    AuthValidationFilter authValidationFilter = new AuthValidationFilter(authFilterHealthCheck);
    authValidationFilter.addEvaluator(new NoAuthEvaluator(), "/", "/swagger.json", "/ui/*", // Handled by Shiro
            "/repo/*" // Cough
    );

    OAuth1Signature verifier = new OAuth1Signature(new OAuthServiceLocatorShim());
    OAuthSecretManager oAuthSecretManager = new OAuthSecretManager() {
        @Override
        public void clearCache() {
        }

        @Override
        public String getSecret(String id) throws AuthValidationException {
            return id.equals(finalConsumerKey) ? finalSecret : null;
        }

        @Override
        public void verifyLastSecretRemovalTime() throws Exception {
        }
    };
    AuthValidator<OAuth1Signature, OAuth1Request> oAuthValidator = new DefaultOAuthValidator(
            Executors.newScheduledThreadPool(1), Long.MAX_VALUE, oAuthSecretManager, 60_000, false, false);
    oAuthValidator.start();
    authValidationFilter.addEvaluator(new NoAuthEvaluator(), "/repo/*", "/amza/rows/stream/*",
            "/amza/rows/taken/*", "/amza/pong/*", "/amza/invalidate/*");
    authValidationFilter.addEvaluator(new OAuthEvaluator(oAuthValidator, verifier), "/upena/*", "/amza/*");

    // TODO something better someday
    String upenaApiUsername = System.getProperty("upena.api.username", null);
    String upenaApiPassword = System.getProperty("upena.api.password", null);

    if (upenaApiUsername != null && upenaApiPassword != null) {
        authValidationFilter.addEvaluator(containerRequestContext -> {
            String authCredentials = containerRequestContext.getHeaderString("Authorization");
            if (authCredentials == null) {
                return AuthStatus.not_handled;
            }

            final String encodedUserPassword = authCredentials.replaceFirst("Basic" + " ", "");
            String usernameAndPassword = null;
            try {
                byte[] decodedBytes = Base64.getDecoder().decode(encodedUserPassword);
                usernameAndPassword = new String(decodedBytes, "UTF-8");
            } catch (IOException e) {
                return AuthStatus.denied;
            }
            final StringTokenizer tokenizer = new StringTokenizer(usernameAndPassword, ":");
            final String username = tokenizer.nextToken();
            final String password = tokenizer.nextToken();

            boolean authenticationStatus = upenaApiUsername.equals(username)
                    && upenaApiPassword.equals(password);

            return authenticationStatus ? AuthStatus.authorized : AuthStatus.denied;
        }, "/api/*");
    }

    jerseyEndpoints.addContainerRequestFilter(authValidationFilter);

    String region = System.getProperty("aws.region", null);
    String roleArn = System.getProperty("aws.roleArn", null);

    AWSClientFactory awsClientFactory = new AWSClientFactory(region, roleArn);

    String accountName = System.getProperty("account.name",
            clusterDiscoveryName == null ? "" : clusterDiscoveryName);
    String humanReadableUpenaClusterName = datacenter + " - " + accountName;
    injectUI(upenaVersion, awsClientFactory, storeMapper, mapper, jvmapi, amzaService, localPathToRepo,
            repositoryProvider, hostKey, ringHost, upenaSSLConfig, port, sessionStore, ubaService, upenaHealth,
            upenaStore, upenaConfigStore, jerseyEndpoints, humanReadableUpenaClusterName, discoveredRoutes);

    injectAmza(baInterner, amzaStats, jerseyEndpoints, amzaService, ringClient);

    InitializeRestfulServer initializeRestfulServer = new InitializeRestfulServer(false, port, "UpenaNode",
            sslEnable, sslKeyStoreAlias, sslKeystorePassword, sslKeystorePath, 128, 10_000);

    buildSwagger();
    initializeRestfulServer.addClasspathResource("/resources");
    initializeRestfulServer.addContextHandler("/", jerseyEndpoints);

    RestfulServer restfulServer = initializeRestfulServer.build();
    restfulServer.start();

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Jetty Service Online");
    LOG.info("-----------------------------------------------------------------------");

    UpenaJerseyEndpoints loopbackJerseyEndpoints = new UpenaJerseyEndpoints(null)
            .addEndpoint(UpenaLoopbackEndpoints.class).addEndpoint(UpenaConfigRestEndpoints.class)
            .addInjectable(SessionStore.class, sessionStore)
            .addInjectable(DiscoveredRoutes.class, discoveredRoutes).addInjectable(upenaConfigStore)
            .addInjectable(upenaStore).addInjectable(upenaHealth)
            .addInjectable(UpenaService.class, upenaService);

    InitializeRestfulServer initializeLoopbackRestfulServer = new InitializeRestfulServer(
            Boolean.parseBoolean(System.getProperty("amza.loopback.strict", "true")), loopbackPort, "UpenaNode",
            false, sslKeyStoreAlias, sslKeystorePassword, sslKeystorePath, 128, 10_000);
    initializeLoopbackRestfulServer.addClasspathResource("/resources");
    initializeLoopbackRestfulServer.addContextHandler("/", loopbackJerseyEndpoints);

    RestfulServer loopbackRestfulServer = initializeLoopbackRestfulServer.build();
    loopbackRestfulServer.start();

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|      Jetty Service Online");
    LOG.info("-----------------------------------------------------------------------");

    if (ubaService != null) {
        Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(() -> {
            try {
                ubaService.nanny();
            } catch (Exception ex) {
                LOG.error("Nanny failure", ex);
            }
        }, 15, 15, TimeUnit.SECONDS);
        LOG.info("-----------------------------------------------------------------------");
        LOG.info("|      Uba Service Online");
        LOG.info("-----------------------------------------------------------------------");
    }
    ubaServiceReference.set(ubaService);

    /*String peers = System.getProperty("manual.peers");
    if (peers != null) {
    String[] hostPortTuples = peers.split(",");
    for (String hostPortTuple : hostPortTuples) {
        String hostPort = hostPortTuple.trim();
        if (hostPort.length() > 0 && hostPort.contains(":")) {
            String[] host_port = hostPort.split(":");
            try {
                UpenaRingHost anotherRingHost = new UpenaRingHost(host_port[0].trim(), Integer.parseInt(host_port[1].trim()));
                List<UpenaRingHost> ring = upenaAmzaService.getRing("master");
                if (!ring.contains(anotherRingHost)) {
                    LOG.info("Adding host to the cluster: " + anotherRingHost);
                    upenaAmzaService.addRingHost("master", anotherRingHost);
                }
            } catch (Exception x) {
                LOG.warn("Malformed hostPortTuple {}", hostPort);
            }
        } else {
            LOG.warn("Malformed hostPortTuple {}", hostPort);
        }
    }
    }*/

    String vpc = System.getProperty("aws.vpc", null);
    UpenaAWSLoadBalancerNanny upenaAWSLoadBalancerNanny = new UpenaAWSLoadBalancerNanny(vpc, upenaStore,
            hostKey, awsClientFactory);

    Executors.newScheduledThreadPool(1).scheduleWithFixedDelay(() -> {
        try {
            upenaAWSLoadBalancerNanny.ensureSelf();
        } catch (Exception x) {
            LOG.warn("Failures while nannying load loadbalancer.", x);
        }
    }, 1, 1, TimeUnit.MINUTES); // TODO better

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|     Waiting for amza to be ready....");
    LOG.info("-----------------------------------------------------------------------");
    while (!amzaService.isReady()) {
        Thread.sleep(1000);
    }

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|     Begin Migration");
    LOG.info("-----------------------------------------------------------------------");

    upenaStore.init(orderIdProvider, Integer.parseInt(System.getProperty("min.service.port", "10000")),
            Integer.parseInt(System.getProperty("max.service.port", String.valueOf(Short.MAX_VALUE))), false);

    upenaConfigStore.init();

    LOG.info("-----------------------------------------------------------------------");
    LOG.info("|     End Migration");
    LOG.info("-----------------------------------------------------------------------");

    addManualPeers(amzaService);

    Host gotHost = upenaStore.hosts.get(hostKey);
    if (gotHost == null || !gotHost.equals(host)) {
        upenaStore.hosts.update(hostKey, host);
    }
}

From source file:io.atomix.protocols.gossip.map.AntiEntropyMapDelegate.java

/**
 * Requests all updates from each peer in the provided list of peers.
 * <p>//from w w w .j  a va 2 s  .  c  o m
 * The returned future will be completed once at least one peer bootstraps this map or bootstrap requests to all peers
 * fail.
 *
 * @param peers the list of peers from which to request updates
 * @return a future to be completed once updates have been received from at least one peer
 */
private CompletableFuture<Void> requestBootstrapFromPeers(List<MemberId> peers) {
    if (peers.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    final int totalPeers = peers.size();
    AtomicBoolean successful = new AtomicBoolean();
    AtomicInteger totalCount = new AtomicInteger();
    AtomicReference<Throwable> lastError = new AtomicReference<>();

    // Iterate through all of the peers and send a bootstrap request. On the first peer that returns
    // a successful bootstrap response, complete the future. Otherwise, if no peers respond with any
    // successful bootstrap response, the future will be completed with the last exception.
    for (MemberId peer : peers) {
        requestBootstrapFromPeer(peer).whenComplete((result, error) -> {
            if (error == null) {
                if (successful.compareAndSet(false, true)) {
                    future.complete(null);
                } else if (totalCount.incrementAndGet() == totalPeers) {
                    Throwable e = lastError.get();
                    if (e != null) {
                        future.completeExceptionally(e);
                    }
                }
            } else {
                if (!successful.get() && totalCount.incrementAndGet() == totalPeers) {
                    future.completeExceptionally(error);
                } else {
                    lastError.set(error);
                }
            }
        });
    }
    return future;
}

From source file:com.microsoft.tfs.client.common.ui.teamexplorer.internal.pendingchanges.PendingChangesViewModel.java

private void loadPendingChangesAndCandidates() {
    if (workspace == null) {
        allPendingChanges = new PendingChange[0];
        updateCandidates(new PendingChange[0]);
        // Also updates checkin note field definitions
        updateIncludedExcludedAndAffected();
        return;/*from www. j  a v a  2s. com*/
    }

    final long start = System.currentTimeMillis();

    final ItemSpec[] itemSpecs = new ItemSpec[1];
    itemSpecs[0] = new ItemSpec(ServerPath.ROOT, RecursionType.FULL);
    final AtomicReference<PendingChange[]> outCandidateChanges = new AtomicReference<PendingChange[]>();

    allPendingChanges = workspace.getPendingChangesWithCandidates(itemSpecs, false, outCandidateChanges);
    updateCandidates(outCandidateChanges.get());

    updateIncludedExcludedAndAffected();

    log.debug(MessageFormat.format("Load changes including candidates took {0} ms", //$NON-NLS-1$
            (System.currentTimeMillis() - start)));
}