Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISPN-3593 Disallow storing a Lucene index when storeAsBinary is enabled #2146

Merged
merged 1 commit into from Oct 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,7 @@
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockFactory;
import org.infinispan.Cache;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.lucene.directory.BuildContext;
import org.infinispan.lucene.locking.BaseLockFactory;
import org.infinispan.lucene.logging.Log;
Expand Down Expand Up @@ -38,9 +39,9 @@ public class DirectoryBuilderImpl implements BuildContext {
private LockFactory lockFactory = null;

public DirectoryBuilderImpl(Cache<?, ?> metadataCache, Cache<?, ?> chunksCache, Cache<?, ?> distLocksCache, String indexName) {
this.metadataCache = checkNoExpiryConfigured(checkNotNull(metadataCache, "metadataCache"), indexName);
this.chunksCache = checkNoExpiryConfigured(checkNotNull(chunksCache, "chunksCache"), indexName);
this.distLocksCache = checkNoExpiryConfigured(checkNotNull(distLocksCache, "distLocksCache"), indexName);
this.metadataCache = checkValidConfiguration(checkNotNull(metadataCache, "metadataCache"), indexName);
this.chunksCache = checkValidConfiguration(checkNotNull(chunksCache, "chunksCache"), indexName);
this.distLocksCache = checkValidConfiguration(checkNotNull(distLocksCache, "distLocksCache"), indexName);
this.indexName = checkNotNull(indexName, "indexName");
}

Expand Down Expand Up @@ -104,13 +105,20 @@ private static <T> T checkNotNull(final T v,final String objectname) {
return v;
}

private static Cache<?, ?> checkNoExpiryConfigured(final Cache<?, ?> cache, String indexName) {
if ( cache != null && cache.getCacheConfiguration().expiration().maxIdle() != -1) {
private static Cache<?, ?> checkValidConfiguration(final Cache<?, ?> cache, String indexName) {
if (cache == null) {
return null;
}
Configuration configuration = cache.getCacheConfiguration();
if (configuration.expiration().maxIdle() != -1) {
throw log.luceneStorageHavingIdleTimeSet( indexName, cache.getName() );
}
if ( cache != null && cache.getCacheConfiguration().expiration().lifespan() != -1) {
if (configuration.expiration().lifespan() != -1) {
throw log.luceneStorageHavingLifespanSet( indexName, cache.getName() );
}
if (configuration.storeAsBinary().enabled()) {
throw log.luceneStorageAsBinaryEnabled(indexName, cache.getName());
}
return cache;
}

Expand Down
Expand Up @@ -78,4 +78,6 @@ public interface Log extends org.infinispan.util.logging.Log {
@Message(value = "'%s' must not be null", id = 15016)
IllegalArgumentException requiredParameterWasPassedNull(String objectname);

@Message(value = "Lucene Directory for index '%s' can not use Cache '%s': store as binary enabled on the Cache configuration!", id = 15017)
IllegalArgumentException luceneStorageAsBinaryEnabled(String indexName, String cacheName);
}
@@ -0,0 +1,66 @@
package org.infinispan.lucene.configuration;

import org.infinispan.Cache;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.lucene.CacheTestSupport;
import org.infinispan.lucene.directory.DirectoryBuilder;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.test.AbstractInfinispanTest;
import org.infinispan.test.TestingUtil;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.testng.annotations.Test;

/**
* Verifies that the Lucene Directory correctly refuses to use a Cache with store as binary enabled.
*
* @author Pedro Ruivo
* @since 6.0
*/
@Test(groups = "functional", testName = "lucene.configuration.NotStoreAsBinaryValidationTest")
public class NotStoreAsBinaryValidationTest extends AbstractInfinispanTest {

private static final String INDEX_NAME = "test-index";
private static final String CACHE_NAME = "test-cache";
private static final String ERROR_MESSAGE_EXP = "ISPN(\\d)*: Lucene Directory for index '" + INDEX_NAME +
"' can not use Cache '" + CACHE_NAME + "': store as binary enabled on the Cache configuration!";

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ERROR_MESSAGE_EXP)
public void failOnStoreKeysAsBinary() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.storeAsBinary().enable().storeKeysAsBinary(true);
failIfStoreAsBinaryEnabled(builder);
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ERROR_MESSAGE_EXP)
public void failOnStoreValuesAsBinary() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.storeAsBinary().enable().storeValuesAsBinary(true);
failIfStoreAsBinaryEnabled(builder);
}

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = ERROR_MESSAGE_EXP)
public void failOnStoreKeysAndValuesAsBinary() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.storeAsBinary().enable()
.storeValuesAsBinary(true)
.storeKeysAsBinary(true);
failIfStoreAsBinaryEnabled(builder);
}

private void failIfStoreAsBinaryEnabled(ConfigurationBuilder configuration) {
EmbeddedCacheManager cacheManager = null;
try {
ConfigurationBuilder cfg = CacheTestSupport.createLocalCacheConfiguration();
cfg.storeAsBinary().enable();
cacheManager = TestCacheManagerFactory.createCacheManager(configuration);
Cache cache = cacheManager.getCache(CACHE_NAME);

DirectoryBuilder.newDirectoryInstance(cache, cache, cache, INDEX_NAME).create();
} finally {
if (cacheManager != null) {
TestingUtil.killCacheManagers(cacheManager);
}
}
}

}