List of usage examples for java.util.concurrent.atomic AtomicReference get
public final V get()
From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.localworkspace.BaselineFolderCollection.java
/** * Given a baseline file GUID, and the target local path where the item will * be placed in the workspace, returns the path (without file extension) * where the baseline file should be created on disk. This method is used to * place new baselines on disk.//from w w w .j a va 2 s .c om * * Do not use this method to look up the location of a baseline that already * exists. * * @param workspace * @param baselineFolders * @param baselineFileGuid * Baseline file GUID for the baseline * @param targetLocalItem * The location on disk of the file * @return The filename on disk where the baseline should be placed (except * for file extension) */ public static String getNewBaselineLocation(final Workspace workspace, final List<BaselineFolder> baselineFolders, final byte[] baselineFileGuid, final String targetLocalItem) { BaselineFolder.checkForValidBaselineFileGUID(baselineFileGuid); BaselineFolder baselineFolder = null; if (targetLocalItem != null && targetLocalItem.length() > 0) { baselineFolder = getBaselineFolderForPartition(baselineFolders, BaselineFolder.getPartitionForPath(targetLocalItem)); } if (null == baselineFolder && baselineFolders.size() > 0) { baselineFolder = baselineFolders.get(0); } final AtomicReference<String> outIndividualBaselineFolder = new AtomicReference<String>(); String toReturn; if (null == baselineFolder) { // There were no baseline folders available to host this baseline. // We will instead store it in our fallback location in the // ProgramData location. BaselineFolder.ensureLocalMetadataDirectoryExists(workspace); toReturn = BaselineFolder.getPathFromGUID(workspace.getLocalMetadataDirectory(), baselineFileGuid, outIndividualBaselineFolder); final File directory = new File(outIndividualBaselineFolder.get()); if (!directory.exists()) { directory.mkdirs(); } return toReturn; } else { BaselineFolder.ensureBaselineDirectoryExists(workspace, baselineFolder.getPath()); toReturn = BaselineFolder.getPathFromGUID(baselineFolder.getPath(), baselineFileGuid, outIndividualBaselineFolder); final File directory = new File(outIndividualBaselineFolder.get()); if (!directory.exists()) { directory.mkdirs(); } return toReturn; } }
From source file:org.elasticsearch.xpack.security.audit.index.AuditTrailTests.java
private Collection<Map<String, Object>> waitForAuditEvents() throws InterruptedException { waitForAuditTrailToBeWritten();/* w w w .j a va 2s.co m*/ final AtomicReference<Collection<Map<String, Object>>> eventsRef = new AtomicReference<>(); awaitBusy(() -> { try { final Collection<Map<String, Object>> events = getAuditEvents(); eventsRef.set(events); return events.size() > 0; } catch (final Exception e) { throw new RuntimeException(e); } }); return eventsRef.get(); }
From source file:io.symcpe.hendrix.nifi.lmm.interceptor.LMMInterceptor.java
@Override public void onTrigger(ProcessContext ctx, ProcessSession session) throws ProcessException { FlowFile flowFile = session.get();/* w w w .j av a2 s . c o m*/ if (flowFile == null) { return; } try { AtomicReference<String> message = new AtomicReference<String>(); session.read(flowFile, true, new InputStreamCallback() { @Override public void process(InputStream in) throws IOException { message.set(IOUtils.toString(in)); } }); flowFile = session.putAttribute(flowFile, "message", message.get()); flowFile = session.putAttribute(flowFile, ATTR_TENANT_ID, ctx.getProperty(TENANT_ID).getValue()); flowFile = session.putAttribute(flowFile, ATTR_API_KEY, ctx.getProperty(API_KEY).getValue()); flowFile = session.putAttribute(flowFile, ATTR_VERSION, _1); String timestamp = ctx.getProperty(TIMESTAMP).evaluateAttributeExpressions(flowFile).getValue(); DateTime ts = formatter.withZoneUTC().parseDateTime(timestamp); flowFile = session.putAttribute(flowFile, ATTR_TIMESTAMP, ts.toString(TARGET_TIMESTAMP_PATTERN, Locale.ENGLISH)); session.transfer(flowFile, SUCCESS); } catch (Exception e) { flowFile = session.putAttribute(flowFile, "Exception", e.getMessage()); session.transfer(flowFile, FAILURE); } }
From source file:com.networknt.client.oauth.OauthHelper.java
/** * De-reference a simple web token to JWT token from OAuth 2.0 provider. This is normally called from the light-router. * * @param derefRequest a DerefRequest object that is constructed from the client.yml file. * @param envTag an environment tag from the server.yml for cluster service lookup. * @return String of JWT token/* www .j a va2s .co m*/ * @throws ClientException when error occurs. */ public static String derefToken(DerefRequest derefRequest, String envTag) throws ClientException { final Http2Client client = Http2Client.getInstance(); final CountDownLatch latch = new CountDownLatch(1); final ClientConnection connection; try { if (derefRequest.getServerUrl() != null) { connection = client.connect(new URI(derefRequest.getServerUrl()), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, derefRequest.enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY) .get(); } else if (derefRequest.getServiceId() != null) { Cluster cluster = SingletonServiceFactory.getBean(Cluster.class); String url = cluster.serviceToUrl("https", derefRequest.getServiceId(), envTag, null); connection = client .connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, derefRequest.enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY) .get(); } else { // both server_url and serviceId are empty in the config. logger.error("Error: both server_url and serviceId are not configured in client.yml for " + derefRequest.getClass()); throw new ClientException("both server_url and serviceId are not configured in client.yml for " + derefRequest.getClass()); } } catch (Exception e) { logger.error("Exception: ", e); throw new ClientException(e); } final AtomicReference<ClientResponse> reference = new AtomicReference<>(); try { ClientRequest request = new ClientRequest().setPath(derefRequest.getUri()).setMethod(Methods.GET); request.getRequestHeaders().put(Headers.AUTHORIZATION, getBasicAuthHeader(derefRequest.getClientId(), derefRequest.getClientSecret())); request.getRequestHeaders().put(Headers.HOST, "localhost"); connection.sendRequest(request, client.createClientCallback(reference, latch)); latch.await(); } catch (Exception e) { logger.error("Exception: ", e); throw new ClientException(e); } finally { IoUtils.safeClose(connection); } return reference.get().getAttachment(Http2Client.RESPONSE_BODY); }
From source file:com.yahoo.pulsar.broker.loadbalance.SimpleLoadManagerImplTest.java
@Test(enabled = true) public void testBasicBrokerSelection() throws Exception { LoadManager loadManager = new SimpleLoadManagerImpl(pulsar1); PulsarResourceDescription rd = new PulsarResourceDescription(); rd.put("memory", new ResourceUsage(1024, 4096)); rd.put("cpu", new ResourceUsage(10, 100)); rd.put("bandwidthIn", new ResourceUsage(250 * 1024, 1024 * 1024)); rd.put("bandwidthOut", new ResourceUsage(550 * 1024, 1024 * 1024)); ResourceUnit ru1 = new SimpleResourceUnit("http://prod2-broker7.messaging.usw.example.com:8080", rd); Set<ResourceUnit> rus = new HashSet<ResourceUnit>(); rus.add(ru1);/*w w w .j a va2 s. c om*/ LoadRanker lr = new ResourceAvailabilityRanker(); AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRankingsInstance = new AtomicReference<>( Maps.newTreeMap()); sortedRankingsInstance.get().put(lr.getRank(rd), rus); Field sortedRankings = SimpleLoadManagerImpl.class.getDeclaredField("sortedRankings"); sortedRankings.setAccessible(true); sortedRankings.set(loadManager, sortedRankingsInstance); ResourceUnit found = ((SimpleLoadManagerImpl) loadManager) .getLeastLoaded(new NamespaceName("pulsar/use/primary-ns.10")); // broker is not active so found should be null assertEquals(found, null, "found a broker when expected none to be found"); }
From source file:com.netflix.config.DynamicContextualPropertyTest.java
@Test public void testCallback() { String json = "[{\"value\":5,\"if\":{\"d1\":[\"v1\",\"v2\"]}, \"comment\": \"some comment\"},{\"value\":10,\"if\":{\"d1\":[\"v3\"],\"d2\":[\"x1\"]}, \"runtimeEval\": true},{\"value\":2}]"; ConfigurationManager.getConfigInstance().setProperty("d1", "v2"); final AtomicReference<Integer> ref = new AtomicReference<Integer>(); DynamicContextualProperty<Integer> prop = new DynamicContextualProperty<Integer>("propWithCallback", 0) { @Override//from ww w . j a v a2s . c o m protected void propertyChanged(Integer newVal) { ref.set(newVal); } }; assertEquals(0, prop.getValue().intValue()); ConfigurationManager.getConfigInstance().setProperty("propWithCallback", json); assertEquals(5, ref.get().intValue()); assertEquals(5, prop.getValue().intValue()); assertEquals("some comment", prop.values.get(0).getComment()); assertTrue(prop.values.get(1).isRuntimeEval()); assertFalse(prop.values.get(0).isRuntimeEval()); // set the property as a single value integer ConfigurationManager.getConfigInstance().setProperty("propWithCallback", "7"); assertEquals(7, ref.get().intValue()); assertEquals(7, prop.getValue().intValue()); }
From source file:com.yahoo.pulsar.broker.loadbalance.SimpleLoadManagerImplTest.java
@Test(enabled = true) public void testPrimary() throws Exception { LoadManager loadManager = new SimpleLoadManagerImpl(pulsar1); PulsarResourceDescription rd = new PulsarResourceDescription(); rd.put("memory", new ResourceUsage(1024, 4096)); rd.put("cpu", new ResourceUsage(10, 100)); rd.put("bandwidthIn", new ResourceUsage(250 * 1024, 1024 * 1024)); rd.put("bandwidthOut", new ResourceUsage(550 * 1024, 1024 * 1024)); ResourceUnit ru1 = new SimpleResourceUnit( "http://" + pulsar1.getAdvertisedAddress() + ":" + pulsar1.getConfiguration().getWebServicePort(), rd);// ww w . j a v a 2 s. c o m Set<ResourceUnit> rus = new HashSet<ResourceUnit>(); rus.add(ru1); LoadRanker lr = new ResourceAvailabilityRanker(); // inject the load report and rankings Map<ResourceUnit, com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport> loadReports = new HashMap<>(); com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport loadReport = new com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport(); loadReport.setSystemResourceUsage(new SystemResourceUsage()); loadReports.put(ru1, loadReport); setObjectField(SimpleLoadManagerImpl.class, loadManager, "currentLoadReports", loadReports); ResourceUnitRanking ranking = new ResourceUnitRanking(loadReport.getSystemResourceUsage(), new HashSet<String>(), new ResourceQuota(), new HashSet<String>(), new ResourceQuota()); Map<ResourceUnit, ResourceUnitRanking> rankings = new HashMap<>(); rankings.put(ru1, ranking); setObjectField(SimpleLoadManagerImpl.class, loadManager, "resourceUnitRankings", rankings); AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRankingsInstance = new AtomicReference<>( Maps.newTreeMap()); sortedRankingsInstance.get().put(lr.getRank(rd), rus); setObjectField(SimpleLoadManagerImpl.class, loadManager, "sortedRankings", sortedRankingsInstance); ResourceUnit found = ((SimpleLoadManagerImpl) loadManager) .getLeastLoaded(new NamespaceName("pulsar/use/primary-ns.10")); // broker is not active so found should be null assertNotEquals(found, null, "did not find a broker when expected one to be found"); }
From source file:com.yahoo.pulsar.broker.loadbalance.SimpleLoadManagerImplTest.java
@Test(enabled = true) public void testDoLoadShedding() throws Exception { LoadManager loadManager = spy(new SimpleLoadManagerImpl(pulsar1)); PulsarResourceDescription rd = new PulsarResourceDescription(); rd.put("memory", new ResourceUsage(1024, 4096)); rd.put("cpu", new ResourceUsage(10, 100)); rd.put("bandwidthIn", new ResourceUsage(250 * 1024, 1024 * 1024)); rd.put("bandwidthOut", new ResourceUsage(550 * 1024, 1024 * 1024)); ResourceUnit ru1 = new SimpleResourceUnit("http://prod1-broker7.messaging.gq1.yahoo.com:8080", rd); ResourceUnit ru2 = new SimpleResourceUnit("http://prod2-broker7.messaging.gq1.yahoo.com:8080", rd); Set<ResourceUnit> rus = new HashSet<ResourceUnit>(); rus.add(ru1);//from ww w .ja v a 2s .c o m rus.add(ru2); LoadRanker lr = new ResourceAvailabilityRanker(); AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRankingsInstance = new AtomicReference<>( Maps.newTreeMap()); sortedRankingsInstance.get().put(lr.getRank(rd), rus); Field sortedRankings = SimpleLoadManagerImpl.class.getDeclaredField("sortedRankings"); sortedRankings.setAccessible(true); sortedRankings.set(loadManager, sortedRankingsInstance); // inject the load report and rankings SystemResourceUsage systemResource = new SystemResourceUsage(); systemResource.setBandwidthIn(new ResourceUsage(90, 100)); Map<String, NamespaceBundleStats> stats = Maps.newHashMap(); NamespaceBundleStats nsb1 = new NamespaceBundleStats(); nsb1.msgRateOut = 10000; NamespaceBundleStats nsb2 = new NamespaceBundleStats(); nsb2.msgRateOut = 10000; stats.put("property/cluster/namespace1/0x00000000_0xFFFFFFFF", nsb1); stats.put("property/cluster/namespace2/0x00000000_0xFFFFFFFF", nsb2); Map<ResourceUnit, com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport> loadReports = new HashMap<>(); com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport loadReport1 = new com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport(); loadReport1.setSystemResourceUsage(systemResource); loadReport1.setBundleStats(stats); com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport loadReport2 = new com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport(); loadReport2.setSystemResourceUsage(new SystemResourceUsage()); loadReport2.setBundleStats(stats); loadReports.put(ru1, loadReport1); loadReports.put(ru2, loadReport2); setObjectField(SimpleLoadManagerImpl.class, loadManager, "currentLoadReports", loadReports); ((SimpleLoadManagerImpl) loadManager).doLoadShedding(); verify(loadManager, atLeastOnce()).doLoadShedding(); }
From source file:de.sainth.recipe.backend.db.repositories.BasicUnitRepository.java
public BasicUnit save(BasicUnit basicUnit) { AtomicReference<BasicUnit> bu = new AtomicReference<>(); create.transaction(configuration -> { using(configuration).insertInto(BASIC_UNITS, BASIC_UNITS.SHORTNAME, BASIC_UNITS.LONGNAME) .values(basicUnit.getShortname(), basicUnit.getLongname()).onConflict().doUpdate() .set(BASIC_UNITS.LONGNAME, basicUnit.getLongname()).execute(); bu.set(using(configuration).selectFrom(BASIC_UNITS) .where(BASIC_UNITS.SHORTNAME.eq(basicUnit.getShortname())).fetchOneInto(BasicUnit.class)); });// w ww.j a v a 2 s . c om return bu.get(); }
From source file:com.yahoo.pulsar.broker.loadbalance.SimpleLoadManagerImplTest.java
@Test(enabled = false) public void testPrimarySecondary() throws Exception { LocalZooKeeperCache mockCache = mock(LocalZooKeeperCache.class); ZooKeeperChildrenCache zooKeeperChildrenCache = mock(ZooKeeperChildrenCache.class); Set<String> activeBrokers = Sets.newHashSet("prod2-broker7.messaging.use.example.com:8080", "prod2-broker8.messaging.use.example.com:8080", "prod2-broker9.messaging.use.example.com:8080"); when(mockCache.getChildren(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT)).thenReturn(activeBrokers); when(zooKeeperChildrenCache.get()).thenReturn(activeBrokers); when(zooKeeperChildrenCache.get(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT)).thenReturn(activeBrokers); Field zkCacheField = PulsarService.class.getDeclaredField("localZkCache"); zkCacheField.setAccessible(true);//from w w w . j a v a 2 s . c o m LocalZooKeeperCache originalLZK1 = (LocalZooKeeperCache) zkCacheField.get(pulsar1); LocalZooKeeperCache originalLZK2 = (LocalZooKeeperCache) zkCacheField.get(pulsar2); System.out.println("lzk are " + originalLZK1.getChildren(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT) + " 2: " + originalLZK2.getChildren(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT)); zkCacheField.set(pulsar1, mockCache); LocalZooKeeperCache newZk = (LocalZooKeeperCache) pulsar1.getLocalZkCache(); System.out.println("lzk mocked are " + newZk.getChildren(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT)); ZooKeeperChildrenCache availableActiveBrokers = new ZooKeeperChildrenCache(pulsar1.getLocalZkCache(), SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT); System.out.println("lzk mocked active brokers are " + availableActiveBrokers.get(SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT)); LoadManager loadManager = new SimpleLoadManagerImpl(pulsar1); PulsarResourceDescription rd = new PulsarResourceDescription(); rd.put("memory", new ResourceUsage(1024, 4096)); rd.put("cpu", new ResourceUsage(10, 100)); rd.put("bandwidthIn", new ResourceUsage(250 * 1024, 1024 * 1024)); rd.put("bandwidthOut", new ResourceUsage(550 * 1024, 1024 * 1024)); ResourceUnit ru1 = new SimpleResourceUnit("http://prod2-broker7.messaging.usw.example.com:8080", rd); Set<ResourceUnit> rus = new HashSet<ResourceUnit>(); rus.add(ru1); LoadRanker lr = new ResourceAvailabilityRanker(); AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRankingsInstance = new AtomicReference<>( Maps.newTreeMap()); sortedRankingsInstance.get().put(lr.getRank(rd), rus); Field sortedRankings = SimpleLoadManagerImpl.class.getDeclaredField("sortedRankings"); sortedRankings.setAccessible(true); sortedRankings.set(loadManager, sortedRankingsInstance); ResourceUnit found = ((SimpleLoadManagerImpl) loadManager) .getLeastLoaded(new NamespaceName("pulsar/use/primary-ns.10")); assertEquals(found.getResourceId(), ru1.getResourceId()); zkCacheField.set(pulsar1, originalLZK1); }