Java Code Examples for com.hazelcast.core.IMap#tryLock()

The following examples show how to use com.hazelcast.core.IMap#tryLock() . 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: SQLDBCache.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
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 2
Source File: SQLDBCache.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
@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 3
Source File: SQLDBCache.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@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 4
Source File: SQLDBCache.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@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;
}