All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bagri.server.hazelcast.task.schema.SchemaProcessor Maven / Gradle / Ivy

The newest version!
package com.bagri.server.hazelcast.task.schema;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.bagri.core.system.Schema;
import com.bagri.server.hazelcast.task.EntityProcessor;
import com.hazelcast.core.IExecutorService;
import com.hazelcast.core.Member;
import com.hazelcast.map.EntryBackupProcessor;
import com.hazelcast.map.EntryProcessor;
import com.hazelcast.spring.context.SpringAware;

@SpringAware
public abstract class SchemaProcessor extends EntityProcessor implements EntryProcessor, 
	EntryBackupProcessor {
	
	protected final transient Logger logger = LoggerFactory.getLogger(getClass());

	protected transient IExecutorService execService;

	public SchemaProcessor() {
		//
	}
	
	public SchemaProcessor(int version, String admin) {
		super(version, admin);
	}
	
    @Autowired
	public void setExecService(IExecutorService execService) {
		this.execService = execService;
		//logger.trace("setSchemaManager; got manager: {}", schemaManager); 
	}

    @Override
	public void processBackup(Entry entry) {
		process(entry);		
	}

	@Override
	public EntryBackupProcessor getBackupProcessor() {
		return this;
	}

	
	protected int initSchemaInCluster(Schema schema) {
		
		logger.trace("initSchemaInCluster.enter; schema: {}", schema);
		SchemaInitiator init = new SchemaInitiator(schema);
		
		int cnt = 0;
        //Set members = hzInstance.getCluster().getMembers();
        //for (Member member: members) {
        //	Future result = execService.submitToMember(init, member);
		//	try {
		//		Boolean ok = result.get();
		//		if (ok) cnt++;
		//		logger.debug("initSchemaInCluster; Schema {}initialized on node {}", ok ? "" : "NOT ", member);
		//	} catch (InterruptedException | ExecutionException ex) {
		//		logger.error("initSchemaInCluster.error; ", ex);
		//	}
        //}
		
		// do this on Schema nodes only, not on ALL nodes!
		Map> result = execService.submitToAllMembers(init);
		for (Map.Entry> entry: result.entrySet()) {
			try {
				Boolean ok = entry.getValue().get();
				if (ok) cnt++;
				logger.debug("initSchemaInCluster; Schema {}initialized on node {}", ok ? "" : "NOT ", entry.getKey());
			} catch (InterruptedException | ExecutionException ex) {
				logger.error("initSchemaInCluster.error; ", ex);
			}
		}

		logger.info("initSchemaInCluster.exit; schema {} initialized on {} nodes", schema, cnt);
		return cnt;
	}
	
	protected int denitSchemaInCluster(Schema schema) {

		logger.trace("denitSchemaInCluster.enter; schema: {}", schema);
		SchemaDenitiator denit = new SchemaDenitiator(schema.getName());
		
		int cnt = 0; 
		Map> result = execService.submitToAllMembers(denit);
		for (Map.Entry> entry: result.entrySet()) {
			try {
				Boolean ok = entry.getValue().get();
				if (ok) cnt++;
				logger.debug("denitSchemaInCluster; Schema {}de-initialized on node {}", ok ? "" : "NOT ", entry.getKey());
			} catch (InterruptedException | ExecutionException ex) {
				logger.error("denitSchemaInCluster.error; ", ex);
			}
		}
		int rcnt = result.size() - cnt;
		logger.info("denitSchemaInCluster.exit; schema {} de-initialized on {} nodes; returning: {}", 
				schema, cnt, rcnt);
		return rcnt;
	}

	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy