Commit de8e0bf2 authored by Brian Long's avatar Brian Long
Browse files

update from refactoring (incomplete)

parent 006597f6
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -33,6 +33,11 @@
	</dependencyManagement>

	<dependencies>
		<dependency>
			<groupId>com.inteligr8.alfresco</groupId>
			<artifactId>cachext-platform-module</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.inteligr8.alfresco</groupId>
			<artifactId>asie-shared</artifactId>
+23 −0
Original line number Diff line number Diff line
package com.inteligr8.alfresco.asie;

public interface CommunityConstants extends Constants {

	static final String BEAN_SHARDSETS_CACHE = "asieShardsetsCache";
	static final String BEAN_NODES_CACHE = "asieNodesCache";
	static final String BEAN_SHARD_NODES_CACHE = "asieShardNodesCache";
	static final String BEAN_SHARDINST_STATE_CACHE = "asieShardInstanceStateCache";
	static final String BEAN_NODE_DISABLE_CACHE = "asieNodeDisabledCache";
	static final String BEAN_NODE_UNAVAIL_CACHE = "asieNodeUnavailableCache";
	static final String BEAN_SHARDINST_DISABLE_CACHE = "asieShardInstanceDisabledCache";
	static final String BEAN_SHARDINST_UNAVAIL_CACHE = "asieShardInstanceUnavailableCache";
	static final String BEAN_CORE_EXPLICIT_CACHE = "asieCoreExplicitCache";

	static final String ATTR_ASIE_SHARDSET = "inteligr8.asie.shardSet";
	static final String ATTR_ASIE_NODE = "inteligr8.asie.node";
	static final String ATTR_ASIE_SHARD_NODES = "inteligr8.asie.shard.nodes";
	static final String ATTR_ASIE_SHARD_NODE = "inteligr8.asie.shard.node";
	static final String ATTR_OBJECT = "object";
	static final String ATTR_DISABLE = "disabled";
	static final String ATTR_NODES = "nodes";

}
+0 −67
Original line number Diff line number Diff line
package com.inteligr8.alfresco.asie.cache;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

import org.alfresco.repo.cache.SimpleCache;

public class MultiValueCache<K extends Serializable, V, C extends Collection<V>> implements SimpleCache<K, C> {
	
	private SimpleCache<K, C> cache;
	private Class<?> collectionType;
	
	public MultiValueCache(SimpleCache<K, C> cache, Class<?> collectionType) {
		this.cache = cache;
		this.collectionType = collectionType;
	}
	
	@SuppressWarnings("unchecked")
	public boolean add(K key, V value) {
		C c = this.cache.get(key);
		if (c != null)
			return c.add(value);
		
		try {
			Constructor<?> constructor = this.collectionType.getConstructor();
			c = (C) constructor.newInstance();
			this.cache.put(key, c);
			return c.add(value);
		} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
			throw new UnsupportedOperationException(e.getMessage(), e);
		}
	}
	
	@Override
	public void clear() {
		this.cache.clear();
	}
	
	@Override
	public C get(K key) {
		return this.cache.get(key);
	}
	
	@Override
	public boolean contains(K key) {
		C c = this.cache.get(key);
		return c == null ? false : !c.isEmpty();
	}
	
	@Override
	public Collection<K> getKeys() {
		return this.cache.getKeys();
	}
	
	@Override
	public void put(K key, C value) {
		this.cache.put(key, value);
	}
	
	@Override
	public void remove(K key) {
		this.cache.remove(key);
	}

}
+4 −59
Original line number Diff line number Diff line
package com.inteligr8.alfresco.asie.service;

import java.io.Serializable;
import java.util.Arrays;

import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.index.shard.ShardInstance;
import org.alfresco.repo.index.shard.ShardState;
import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.service.cmr.attributes.AttributeService.AttributeQueryCallback;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.inteligr8.alfresco.asie.Constants;
import com.inteligr8.alfresco.asie.enterprise.EnterpriseConstants;

@Component
public class ShardStateService implements com.inteligr8.alfresco.asie.spi.ShardStateService {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	@Qualifier(Constants.QUALIFIER_ASIE)
	private AttributeService attrService;
	
	@Autowired
	@Qualifier(Constants.BEAN_SHARD_STATE_CACHE)
    private SimpleCache<ShardInstance, ShardState> shardStateCache;
	private SolrShardRegistry shardRegistry;
	
	@Override
	public void clear() {
		this.logger.info("Removing all nodes/shards from the shard registry");
		
		// this clears the state from the backend database
		this.attrService.removeAttributes(EnterpriseConstants.ATTR_SHARD_STATE);
		this.attrService.removeAttributes(EnterpriseConstants.ATTR_SHARD_SUBSCRIPTION);
		this.attrService.removeAttributes(Constants.ATTR_ASIE);
		
		// this clears the state from Hazelcast
		this.shardStateCache.clear();
		this.shardToGuidCache.clear();
	}
	
	public void remove(Serializable... keys) {
		if (keys.length == 0)
			throw new IllegalArgumentException();
		
		this.logger.info("Removing from the shard registry: {}", Arrays.toString(keys));

		Serializable[] shardStateKeys = keys;
		Serializable[] shardSubKeys;
		if (EnterpriseConstants.ATTR_SHARD_STATE.equals(keys[0])) {
			shardSubKeys = ArrayUtils.clone(keys);
			shardSubKeys[0] = EnterpriseConstants.ATTR_SHARD_SUBSCRIPTION;
		} else {
			shardStateKeys = ArrayUtils.addFirst(keys, EnterpriseConstants.ATTR_SHARD_STATE);
			shardSubKeys = ArrayUtils.addFirst(keys, EnterpriseConstants.ATTR_SHARD_SUBSCRIPTION);
		}
		
		ShardState shardState = (ShardState) this.attrService.getAttribute(shardStateKeys);

		// this clears the state from the backend database
		this.attrService.removeAttribute(shardStateKeys);
		this.attrService.removeAttribute(shardSubKeys);

		// this clears the state from Hazelcast
		if (shardState != null) {
			this.shardStateCache.remove(shardState.getShardInstance());
			this.shardToGuidCache.remove(shardState.getShardInstance());
		}
	}
	
	public void iterate(AttributeQueryCallback callback) {
		this.attrService.getAttributes(callback, EnterpriseConstants.ATTR_SHARD_STATE);
		this.shardRegistry.purge();
	}

}
+363 −212

File changed.

Preview size limit exceeded, changes collapsed.

Loading