List of usage examples for java.util Collections synchronizedSet
public static <T> Set<T> synchronizedSet(Set<T> s)
From source file:tachyon.master.MasterInfo.java
public MasterInfo(InetSocketAddress address, Journal journal, ExecutorService executorService, TachyonConf tachyonConf) throws IOException { mExecutorService = executorService;//from w ww . ja v a2 s. com mTachyonConf = tachyonConf; mUFSDataFolder = mTachyonConf.get(Constants.UNDERFS_DATA_FOLDER, Constants.DEFAULT_DATA_FOLDER); mRawTables = new RawTables(mTachyonConf); mRoot = new InodeFolder("", mInodeCounter.incrementAndGet(), -1, System.currentTimeMillis()); mFileIdToInodes.put(mRoot.getId(), mRoot); mMasterAddress = address; mStartTimeMs = System.currentTimeMillis(); // TODO This name need to be changed. mStartTimeNSPrefix = mStartTimeMs - (mStartTimeMs % 1000000); mJournal = journal; mWhitelist = new PrefixList( mTachyonConf.getList(Constants.MASTER_WHITELIST, ",", new LinkedList<String>())); mPinnedInodeFileIds = Collections.synchronizedSet(new HashSet<Integer>()); mJournal.loadImage(this); }
From source file:org.apache.hadoop.yarn.server.resourcemanager.security.DelegationTokenRenewer.java
private void handleAppSubmitEvent(AbstractDelegationTokenRenewerAppEvent evt) throws IOException, InterruptedException { ApplicationId applicationId = evt.getApplicationId(); Credentials ts = evt.getCredentials(); boolean shouldCancelAtEnd = evt.shouldCancelAtEnd(); if (ts == null) { return; // nothing to add }/* w w w . j a v a 2 s .co m*/ if (LOG.isDebugEnabled()) { LOG.debug("Registering tokens for renewal for:" + " appId = " + applicationId); } Collection<Token<?>> tokens = ts.getAllTokens(); long now = System.currentTimeMillis(); // find tokens for renewal, but don't add timers until we know // all renewable tokens are valid // At RM restart it is safe to assume that all the previously added tokens // are valid appTokens.put(applicationId, Collections.synchronizedSet(new HashSet<DelegationTokenToRenew>())); Set<DelegationTokenToRenew> tokenList = new HashSet<DelegationTokenToRenew>(); boolean hasHdfsToken = false; for (Token<?> token : tokens) { if (token.isManaged()) { if (token.getKind().equals(HDFS_DELEGATION_KIND)) { LOG.info(applicationId + " found existing hdfs token " + token); hasHdfsToken = true; } if (skipTokenRenewal(token)) { continue; } DelegationTokenToRenew dttr = allTokens.get(token); if (dttr == null) { dttr = new DelegationTokenToRenew(Arrays.asList(applicationId), token, getConfig(), now, shouldCancelAtEnd, evt.getUser()); try { renewToken(dttr); } catch (IOException ioe) { if (ioe instanceof SecretManager.InvalidToken && dttr.maxDate < Time.now() && evt instanceof DelegationTokenRenewerAppRecoverEvent && token.getKind().equals(HDFS_DELEGATION_KIND)) { LOG.info("Failed to renew hdfs token " + dttr + " on recovery as it expired, requesting new hdfs token for " + applicationId + ", user=" + evt.getUser(), ioe); requestNewHdfsDelegationTokenAsProxyUser(Arrays.asList(applicationId), evt.getUser(), evt.shouldCancelAtEnd()); continue; } throw new IOException("Failed to renew token: " + dttr.token, ioe); } } tokenList.add(dttr); } } if (!tokenList.isEmpty()) { // Renewing token and adding it to timer calls are separated purposefully // If user provides incorrect token then it should not be added for // renewal. for (DelegationTokenToRenew dtr : tokenList) { DelegationTokenToRenew currentDtr = allTokens.putIfAbsent(dtr.token, dtr); if (currentDtr != null) { // another job beat us currentDtr.referringAppIds.add(applicationId); appTokens.get(applicationId).add(currentDtr); } else { appTokens.get(applicationId).add(dtr); setTimerForTokenRenewal(dtr); } } } if (!hasHdfsToken) { requestNewHdfsDelegationTokenAsProxyUser(Arrays.asList(applicationId), evt.getUser(), shouldCancelAtEnd); } }
From source file:org.exoplatform.services.jcr.ext.backup.impl.BackupManagerImpl.java
/** * BackupManagerImpl constructor./*from w w w . j a va 2 s.c o m*/ * * InitParams, the init parameters * @param repoService * RepositoryService, the repository service * @param registryService * RegistryService, the registry service */ public BackupManagerImpl(ExoContainerContext ctx, InitParams initParams, RepositoryService repoService, RegistryService registryService) { this.messagesListener = new MessagesListener(); this.repoService = repoService; this.registryService = registryService; this.initParams = initParams; this.tempDir = new File(PrivilegedSystemHelper.getProperty("java.io.tmpdir")); currentBackups = Collections.synchronizedSet(new HashSet<BackupChain>()); currentRepositoryBackups = Collections.synchronizedSet(new HashSet<RepositoryBackupChain>()); messages = new BackupMessagesLog(MESSAGES_MAXSIZE); this.restoreJobs = new ArrayList<JobWorkspaceRestore>(); this.restoreRepositoryJobs = new CopyOnWriteArrayList<JobRepositoryRestore>(); this.workspaceBackupStopper = new WorkspaceBackupAutoStopper(ctx); this.repositoryBackupStopper = new RepositoryBackupAutoStopper(ctx); }
From source file:com.smartitengineering.cms.spi.impl.content.RubyGeneratorTest.java
@Test public void testMultiRubyRepGeneration() throws IOException { TypeRepresentationGenerator generator = new RubyRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override//from w w w . ja va 2 s. co m public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.RUBY, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); final WorkspaceAPI api = impl; registerBeanFactory(api); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final Map<String, Field> fieldMap = mockery.mock(Map.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.RUBY)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils.toByteArray( getClass().getClassLoader().getResourceAsStream("scripts/ruby/test-script.rb")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(fieldMap).get(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getFields(); will(returnValue(fieldMap)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:com.smartitengineering.cms.spi.impl.content.JavascriptGeneratorTest.java
@Test public void testMultiJavascriptRepGeneration() throws IOException { TypeRepresentationGenerator generator = new JavascriptRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override/*from ww w . j a v a 2s.co m*/ public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.JAVASCRIPT, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); registerBeanFactory(impl); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final Map<String, Field> fieldMap = mockery.mock(Map.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.JAVASCRIPT)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils .toByteArray(getClass().getClassLoader().getResourceAsStream("scripts/js/test-script.js")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(fieldMap).get(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getFields(); will(returnValue(fieldMap)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); Assert.assertNotNull(SmartContentAPI.getInstance()); Assert.assertNotNull(SmartContentAPI.getInstance().getContentLoader()); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:com.xerox.amazonws.simpledb.Domain.java
/** * Returns an named item.//from w w w. j a v a 2 s .c o m * * @param identifier the name of the item to be deleted * @throws SDBException wraps checked exceptions */ public Result<Item> getItem(String identifier) throws SDBException { if (cache != null) { Item cached = cache.getItem(identifier); if (cached != null) { return new Result<Item>(null, cached); } // else, go fetch it anyway } Map<String, String> params = new HashMap<String, String>(); params.put("DomainName", domainName); params.put("ItemName", identifier); HttpGet method = new HttpGet(); GetAttributesResponse response = makeRequestInt(method, "GetAttributes", params, GetAttributesResponse.class); Item newItem = new ItemVO(identifier); Map<String, Set<String>> attrs = newItem.getAttributes(); for (Attribute a : response.getGetAttributesResult().getAttributes()) { String name = a.getName().getValue(); String encoding = a.getName().getEncoding(); if (encoding != null && encoding.equals("base64")) { name = new String(Base64.decodeBase64(name.getBytes())); } String value = a.getValue().getValue(); encoding = a.getValue().getEncoding(); if (encoding != null && encoding.equals("base64")) { value = new String(Base64.decodeBase64(value.getBytes())); } Set<String> vals = attrs.get(name); if (vals == null) { vals = Collections.synchronizedSet(new HashSet<String>()); attrs.put(name, vals); } vals.add(value); } if (cache != null) { cache.putItem(newItem); } return new Result<Item>(null, newItem); }
From source file:de.uni_luebeck.inb.knowarc.usecases.invocation.local.LocalUseCaseInvocation.java
@Override public void rememberRun(String runId) { this.setRunId(runId); Set<String> directories = runIdToTempDir.get(runId); if (directories == null) { directories = Collections.synchronizedSet(new HashSet<String>()); runIdToTempDir.put(runId, directories); }//from w w w .ja v a2s. c om try { directories.add(tempDir.getCanonicalPath()); } catch (IOException e) { logger.error("Unable to record temporary directory: " + tempDir, e); } }
From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll(java.util.Collection)} * using multiple read threads.//ww w.j a va 2s . c o m * <p/> * Two read threads should block on an empty buffer until a collection with two distinct objects is added then both * threads should complete. Each thread should have read a different object. * @throws InterruptedException */ @Test // @Schedules({ // @Schedule(name = "BlockedRemoveWithAddAll2", sequence = "[beforeNullRemove: afterNullRemove]@readThread1->beforeAddAll@main," + // "[beforeNullRemove: afterNullRemove]@readThread2->beforeAddAll@main," + // "afterNullRemove@readThread1->afterAddAll@main," + "afterNullRemove@readThread2->afterAddAll@main") }) public void testBlockedRemoveWithAddAll2() throws InterruptedException { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj1 = new Object(); Object obj2 = new Object(); Set objs = Collections.synchronizedSet(new HashSet()); objs.add(obj1); objs.add(obj2); // run methods will remove and compare -- must wait for addAll Thread thread1 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread1"); Thread thread2 = new ReadThread(blockingBuffer, objs, "remove", "BlockedRemoveWithAddAll2", "readThread2"); thread1.start(); thread2.start(); // give hungry read threads ample time to hang try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } /* @Event("beforeAddAll")*/ blockingBuffer.addAll(objs); // allow notified threads to complete try { Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } /* @Event("afterAddAll")*/ assertEquals("BlockedRemoveWithAddAll2", 0, objs.size()); // There should not be any threads waiting. thread1.join(); thread2.join(); //assertFalse( "BlockedRemoveWithAddAll1", thread1.isAlive() || thread2.isAlive() ); //if( thread1.isAlive() || thread2.isAlive() ) { //fail( "Live thread(s) when both should be dead." ); //} }
From source file:com.smartitengineering.cms.spi.impl.content.GroovyGeneratorTest.java
@Test public void testMultiGroovyRepGeneration() throws IOException { TypeRepresentationGenerator generator = new GroovyRepresentationGenerator(); final RepresentationTemplate template = mockery.mock(RepresentationTemplate.class); WorkspaceAPIImpl impl = new WorkspaceAPIImpl() { @Override/*from w ww.j ava2s. c o m*/ public RepresentationTemplate getRepresentationTemplate(WorkspaceId id, String name) { return template; } }; impl.setRepresentationGenerators(Collections.singletonMap(TemplateType.GROOVY, generator)); final RepresentationProvider provider = new RepresentationProviderImpl(); final WorkspaceAPI api = impl; registerBeanFactory(api); final Content content = mockery.mock(Content.class); final Field field = mockery.mock(Field.class); final FieldValue value = mockery.mock(FieldValue.class); final ContentType type = mockery.mock(ContentType.class); final Map<String, RepresentationDef> reps = mockery.mock(Map.class, "repMap"); final RepresentationDef def = mockery.mock(RepresentationDef.class); final int threadCount = new Random().nextInt(100); logger.info("Number of parallel threads " + threadCount); mockery.checking(new Expectations() { { exactly(threadCount).of(template).getTemplateType(); will(returnValue(TemplateType.GROOVY)); exactly(threadCount).of(template).getTemplate(); final byte[] toByteArray = IOUtils.toByteArray(getClass().getClassLoader() .getResourceAsStream("scripts/groovy/GroovyTestRepresentationGenerator.groovy")); will(returnValue(toByteArray)); exactly(threadCount).of(template).getName(); will(returnValue(REP_NAME)); for (int i = 0; i < threadCount; ++i) { exactly(1).of(value).getValue(); will(returnValue(String.valueOf(i))); } exactly(threadCount).of(field).getValue(); will(returnValue(value)); exactly(threadCount).of(content).getField(with(Expectations.<String>anything())); will(returnValue(field)); exactly(threadCount).of(content).getContentDefinition(); will(returnValue(type)); final ContentId contentId = mockery.mock(ContentId.class); exactly(2 * threadCount).of(content).getContentId(); will(returnValue(contentId)); final WorkspaceId wId = mockery.mock(WorkspaceId.class); exactly(threadCount).of(contentId).getWorkspaceId(); will(returnValue(wId)); exactly(2 * threadCount).of(type).getRepresentationDefs(); will(returnValue(reps)); exactly(2 * threadCount).of(reps).get(with(REP_NAME)); will(returnValue(def)); exactly(threadCount).of(def).getParameters(); will(returnValue(Collections.emptyMap())); exactly(threadCount).of(def).getMIMEType(); will(returnValue(GroovyGeneratorTest.MIME_TYPE)); final ResourceUri rUri = mockery.mock(ResourceUri.class); exactly(threadCount).of(def).getResourceUri(); will(returnValue(rUri)); exactly(threadCount).of(rUri).getValue(); will(returnValue("iUri")); } }); final Set<String> set = Collections.synchronizedSet(new LinkedHashSet<String>(threadCount)); final List<String> list = Collections.synchronizedList(new ArrayList<String>(threadCount)); final AtomicInteger integer = new AtomicInteger(0); Threads group = new Threads(); for (int i = 0; i < threadCount; ++i) { group.addThread(new Thread(new Runnable() { public void run() { Representation representation = provider.getRepresentation(REP_NAME, type, content); Assert.assertNotNull(representation); Assert.assertEquals(REP_NAME, representation.getName()); final String rep = StringUtils.newStringUtf8(representation.getRepresentation()); list.add(rep); set.add(rep); Assert.assertEquals(GroovyGeneratorTest.MIME_TYPE, representation.getMimeType()); integer.addAndGet(1); } })); } group.start(); try { group.join(); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } logger.info("Generated reps list: " + list); logger.info("Generated reps set: " + set); Assert.assertEquals(threadCount, integer.get()); Assert.assertEquals(threadCount, list.size()); Assert.assertEquals(threadCount, set.size()); }
From source file:org.alinous.script.runtime.VariableRepository.java
private void writeVariableInFinal(Element repositoryElement, PostContext context) { VariableRepository frepo = null;/*from www . j av a 2 s . c o m*/ synchronized (this.finalRepositoryMap) { frepo = this.finalRepositoryMap.get(context); } if (frepo == null) { return; } Iterator<String> it = Collections.synchronizedSet(frepo.variables.keySet()).iterator(); while (it.hasNext()) { String prop = it.next(); if (this.variables.containsKey(prop)) { continue; } IScriptVariable val = frepo.variables.get(prop); val.exportIntoJDomElement(repositoryElement); } }