Java Code Examples for com.hazelcast.core.IMap#unlock()
The following examples show how to use
com.hazelcast.core.IMap#unlock() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HazelcastTransactionManager.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override protected void lock(TransactionDefinition definition) { Instant now = Instant.now(); String name = definition.getName(); final IMap<String, HazelcastTransactionDefinition> store = getStore(); try { store.lock(name); HazelcastTransactionDefinition current = store.get(name); if (current == null) { store.put(name, new HazelcastTransactionDefinition(definition)); } else if (now.isAfter(current.getMost())) { store.put(name, new HazelcastTransactionDefinition(definition)); } else { throw new TransactionLockException(); } } finally { store.unlock(name); } }
Example 2
Source File: HazelcastTransactionManager.java From jstarcraft-core with Apache License 2.0 | 6 votes |
@Override protected void unlock(TransactionDefinition definition) { Instant now = Instant.now(); String name = definition.getName(); final IMap<String, HazelcastTransactionDefinition> store = getStore(); try { store.lock(name); HazelcastTransactionDefinition current = store.get(name); if (current == null) { throw new TransactionUnlockException(); } else if (now.isAfter(current.getMost())) { throw new TransactionUnlockException(); } else { store.remove(name); } } finally { store.unlock(name); } }
Example 3
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
@Override public void updateCacheItem(CacheItem cacheItem) { String hashedSignature = cacheItem.getSignature(); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter try { if (containsCacheItem(hashedSignature, true)) { cacheDao.updateCacheItem(cacheItem); logger.debug("The dataset with hash [" + hashedSignature + "] has been updated"); } else { logger.debug("The dataset with hash [" + hashedSignature + "] does not exist in cache"); } } finally { mapLocks.unlock(hashedSignature); } }
Example 4
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
@Override public void removeCacheItem(String signature) { String hashedSignature = Helper.sha256(signature); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter try { if (containsCacheItem(signature)) { cacheDao.deleteCacheItemBySignature(hashedSignature); logger.debug("The dataset with signature[" + signature + "] and hash [" + hashedSignature + "] has been updated"); } else { logger.debug("The dataset with signature[" + signature + "] and hash [" + hashedSignature + "] does not exist in cache"); } } finally { mapLocks.unlock(hashedSignature); } }
Example 5
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
public void removeCacheItem(String signature, boolean isHash) { if (isHash) { IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(signature); // it is possible to use also the method tryLock(...) with timeout parameter try { if (containsCacheItem(signature, true)) { cacheDao.deleteCacheItemBySignature(signature); logger.debug("The dataset with hash [" + signature + "] has been deleted"); } else { logger.debug("The dataset with hash [" + signature + "] does not exist in cache"); } } finally { mapLocks.unlock(signature); } } else { removeCacheItem(signature); } }
Example 6
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
@Override public CacheItem getCacheItem(String resultSetSignature) { CacheItem cacheItem = null; String hashedSignature = Helper.sha256(resultSetSignature); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter try { cacheItem = cacheDao.loadCacheItemBySignature(hashedSignature); if (cacheItem != null) { logger.debug("The dataset with signature[" + resultSetSignature + "] and hash [" + hashedSignature + "] has been found in cache"); } else { logger.debug("The dataset with signature[" + resultSetSignature + "] and hash [" + hashedSignature + "] does not exist in cache"); } } finally { mapLocks.unlock(hashedSignature); } return cacheItem; }
Example 7
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
public CacheItem getCacheItem(String signature, boolean isHash) { if (isHash) { CacheItem cacheItem = null; IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(signature); // it is possible to use also the method tryLock(...) with timeout parameter try { cacheItem = cacheDao.loadCacheItemBySignature(signature); if (cacheItem != null) { logger.debug("The dataset with hash [" + signature + "] has been found in cache"); } else { logger.debug("The dataset with hash [" + signature + "] does not exist in cache"); } } finally { mapLocks.unlock(signature); } return cacheItem; } else { return getCacheItem(signature); } }
Example 8
Source File: HazelcastLockProvider.java From ShedLock with Apache License 2.0 | 6 votes |
@Override @NonNull public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) { log.trace("lock - Attempt : {}", lockConfiguration); final Instant now = ClockProvider.now(); final String lockName = lockConfiguration.getName(); final IMap<String, HazelcastLock> store = getStore(); try { // lock the map key entry store.lock(lockName, keyLockTime(lockConfiguration), TimeUnit.MILLISECONDS); // just one thread at a time, in the cluster, can run this code // each thread waits until the lock to be unlock if (tryLock(lockConfiguration, now)) { return Optional.of(new HazelcastSimpleLock(this, lockConfiguration)); } } finally { // released the map lock for the others threads store.unlock(lockName); } return Optional.empty(); }
Example 9
Source File: SQLDBCacheMetadata.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void addCacheItem(String dataSetName, String resultsetSignature, Map<String, Object> properties, String tableName, BigDecimal dimension) { String hashedSignature = Helper.sha256(resultsetSignature); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); mapLocks.lock(hashedSignature); // it is possible to use also the method tryLock(...) with timeout parameter try { removeCacheItem(resultsetSignature); CacheItem item = new CacheItem(); item.setName(dataSetName); item.setTable(tableName); item.setSignature(hashedSignature); item.setDimension(dimension); Date now = new Date(); item.setCreationDate(now); item.setLastUsedDate(now); item.setProperties(properties); cacheDao.insertCacheItem(item); logger.debug("Added cacheItem : [ Name: " + item.getName() + " \n Signature: " + item.getSignature() + " \n Dimension: " + item.getDimension() + " bytes (approximately) ]"); } finally { mapLocks.unlock(hashedSignature); } }
Example 10
Source File: SQLDBCache.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
private boolean dropTableAndRemoveCacheItem(String signature, boolean isHash) { logger.debug("IN"); String hashedSignature; if (isHash) { hashedSignature = signature; logger.debug("Delete dataset with hash [" + signature + "]"); } else { hashedSignature = Helper.sha256(signature); logger.debug("Delete dataset with signature [" + signature + "] and hash [" + hashedSignature + "]"); } IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); try { if (mapLocks.tryLock(hashedSignature, getTimeout(), TimeUnit.SECONDS, getLeaseTime(), TimeUnit.SECONDS)) { try { if (getMetadata().containsCacheItem(signature, isHash)) { PersistedTableManager persistedTableManager = new PersistedTableManager(); String tableName = getMetadata().getCacheItem(signature, isHash).getTable(); persistedTableManager.dropTableIfExists(getDataSource(), tableName); getMetadata().removeCacheItem(signature, isHash); logger.debug("Removed table " + tableName + " from [SQLDBCache] corresponding to the result Set: " + signature); logger.debug("Deleted"); return true; } else { logger.debug("Not deleted, dataset not in cache"); return false; } } finally { mapLocks.unlock(hashedSignature); } } else { logger.debug("Impossible to acquire the lock for dataset [" + hashedSignature + "]. Timeout. Returning false."); } } catch (InterruptedException e) { logger.debug("The current thread has failed to release the lock for dataset [" + hashedSignature + "] in time. Returning a null datastore.", e); } logger.debug("OUT"); return false; }
Example 11
Source File: SQLDBCache.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
@Override public void update(String hashedSignature, IDataStore dataStore) { logger.trace("IN"); logger.debug("Dataset has #" + dataStore.getMetaData().getFieldCount() + " fields. The Dataset will be persisted."); Monitor timing = MonitorFactory.start("Knowage.SQLDBCache.put:gettingMap"); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); timing.stop(); try { timing = MonitorFactory.start("Knowage.SQLDBCache.put:gettingLock[" + hashedSignature + "]"); if (mapLocks.tryLock(hashedSignature, getTimeout(), TimeUnit.SECONDS, getLeaseTime(), TimeUnit.SECONDS)) { timing.stop(); try { timing = MonitorFactory.start("Knowage.SQLDBCache.put:usingLock[" + hashedSignature + "]"); // check again it is not already inserted CacheItem cacheItem = getMetadata().getCacheItem(hashedSignature, true); Assert.assertNotNull(cacheItem, "Cannot find a cache item for [" + hashedSignature + "]"); updateStoreInCache(cacheItem, dataStore); cacheItem.setCreationDate(new Date()); getMetadata().updateCacheItem(cacheItem); } finally { timing.stop(); mapLocks.unlock(hashedSignature); } } else { timing.stop(); logger.debug("Impossible to acquire the lock for dataset [" + hashedSignature + "]. Timeout."); } } catch (InterruptedException e) { logger.debug("The current thread has failed to release the lock for dataset [" + hashedSignature + "] in time.", e); } finally { logger.debug("OUT"); } }
Example 12
Source File: SQLDBCache.java From Knowage-Server with GNU Affero General Public License v3.0 | 4 votes |
@Override public IDataStore get(String signature, boolean isHash) { logger.debug("IN"); IDataStore dataStore = null; String hashedSignature = isHash ? signature : Helper.sha256(signature); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); try { if (mapLocks.tryLock(hashedSignature, getTimeout(), TimeUnit.SECONDS, getLeaseTime(), TimeUnit.SECONDS)) { try { if (getMetadata().containsCacheItem(signature, isHash)) { logger.debug("Resultset with signature [" + signature + "] found"); CacheItem cacheItem = getMetadata().getCacheItem(signature, isHash); cacheItem.setLastUsedDate(new Date()); // update DB information about this cacheItem getMetadata().updateCacheItem(cacheItem); String tableName = cacheItem.getTable(); logger.debug("The table associated to signature [" + signature + "] is [" + tableName + "]"); dataStore = dataSource.executeStatement("SELECT * FROM " + tableName, 0, 0); } else { logger.debug("Resultset with signature [" + signature + "] not found"); } } catch (Throwable t) { if (t instanceof CacheException) throw (CacheException) t; else throw new CacheException("An unexpected error occure while getting dataset from cache", t); } finally { mapLocks.unlock(hashedSignature); } } else { logger.debug("Impossible to acquire the lock for dataset [" + signature + "]. Timeout. Returning a null datastore."); } } catch (InterruptedException e) { logger.debug("The current thread has failed to release the lock for dataset [" + hashedSignature + "] in time. Returning a null datastore.", e); } logger.debug("OUT"); return dataStore; }
Example 13
Source File: SQLDBCache.java From Knowage-Server with GNU Affero General Public License v3.0 | 4 votes |
@SuppressWarnings("unchecked") private IDataStore queryStandardCachedDataset(IDataSet dataSet, String resultsetSignature, List<AbstractSelectionField> projections, Filter filter, List<AbstractSelectionField> groups, List<Sorting> sortings, List<List<AbstractSelectionField>> summaryRowProjections, int offset, int fetchSize, int maxRowCount, Set<String> indexes) throws DataBaseException { IDataStore toReturn = null; String hashedSignature = Helper.sha256(resultsetSignature); Monitor timing = MonitorFactory.start("Knowage.SQLDBCache.queryStandardCachedDataset:gettingMap"); IMap mapLocks = DistributedLockFactory.getDistributedMap(SpagoBIConstants.DISTRIBUTED_MAP_INSTANCE_NAME, SpagoBIConstants.DISTRIBUTED_MAP_FOR_CACHE); timing.stop(); try { timing = MonitorFactory.start("Knowage.SQLDBCache.queryStandardCachedDataset:gettingLock[" + hashedSignature + "]"); if (mapLocks.tryLock(hashedSignature, getTimeout(), TimeUnit.SECONDS, getLeaseTime(), TimeUnit.SECONDS)) { timing.stop(); cacheMetadata.updateAllCacheItems(getDataSource()); try { timing = MonitorFactory.start("Knowage.SQLDBCache.queryStandardCachedDataset:usingLock[" + hashedSignature + "]"); if (getMetadata().containsCacheItem(resultsetSignature)) { logger.debug("Found dataset with signature [" + resultsetSignature + "] and hash [" + hashedSignature + "] inside the cache"); CacheItem cacheItem = getMetadata().getCacheItem(resultsetSignature); cacheItem.setLastUsedDate(new Date()); getMetadata().updateCacheItem(cacheItem); // update DB information about this cacheItem String tableName = cacheItem.getTable(); logger.debug("Found resultSet with signature [" + resultsetSignature + "] inside the Cache, table used [" + tableName + "]"); FlatDataSet flatDataSet = new FlatDataSet(); flatDataSet.setDataSource(dataSource); flatDataSet.setTableName(tableName); flatDataSet.setMetadata(dataSet.getMetadata()); IDatasetEvaluationStrategy strategy = DatasetEvaluationStrategyFactory.get(DatasetEvaluationStrategyType.FLAT, flatDataSet, null); toReturn = strategy.executeQuery(projections, filter, groups, sortings, summaryRowProjections, offset, fetchSize, maxRowCount, indexes); toReturn.setCacheDate(cacheItem.getCreationDate()); /* CHECK IF INDEXES EXIST OR CREATE THEM */ if (indexes != null) { Iterator<String> it = indexes.iterator(); while (it.hasNext()) { String currInd = it.next(); Set<String> currIndSet = new HashSet<String>(); currIndSet.add(currInd); PersistedTableManager persistedTableManager = new PersistedTableManager(); persistedTableManager.setTableName(tableName); persistedTableManager.setDialect(DatabaseDialect.get(getDataSource().getHibDialectClass())); persistedTableManager.setRowCountColumIncluded(false); int queryTimeout; try { queryTimeout = Integer .parseInt(SingletonConfig.getInstance().getConfigValue("SPAGOBI.CACHE.CREATE_AND_PERSIST_TABLE.TIMEOUT")); } catch (NumberFormatException nfe) { logger.debug("The value of SPAGOBI.CACHE.CREATE_AND_PERSIST_TABLE.TIMEOUT config must be an integer"); queryTimeout = -1; } if (queryTimeout > 0) { logger.debug("Setting query timeout..."); persistedTableManager.setQueryTimeout(queryTimeout); } persistedTableManager.createIndexesOnTable(dataSet, dataSource, tableName, currIndSet); } } } else { logger.debug("Cannot find dataset with signature [" + resultsetSignature + "] and hash [" + hashedSignature + "] inside the cache"); } } finally { timing.stop(); mapLocks.unlock(hashedSignature); } } else { timing.stop(); logger.debug("Impossible to acquire the lock for dataset [" + hashedSignature + "]. Timeout. Returning a null datastore."); } } catch (InterruptedException e) { logger.debug("The current thread has failed to release the lock for dataset [" + hashedSignature + "] in time. Returning a null datastore.", e); } logger.debug("OUT"); return toReturn; }