/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see .
*/
package org.alfresco.repo.index.shard;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import org.alfresco.repo.cache.SimpleCache;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.attributes.AttributeService;
import org.alfresco.service.cmr.attributes.AttributeService.AttributeQueryCallback;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.GUID;
import org.alfresco.util.Pair;
import com.hazelcast.util.ConcurrentHashSet;
/**
* @author Andy
*/
public class ShardRegistryImpl implements ShardRegistry
{
/**
* The best shard sould be at the top;
* @author Andy
*
*/
public static class FlocComparator implements Comparator>>>
{
public FlocComparator()
{
}
@Override
public int compare(Pair>> left, Pair>> right)
{
double leftTxCount = 0;
for(HashSet states : left.getSecond().values())
{
long shardMaxTxCount = 0;
for(ShardState state : states)
{
shardMaxTxCount = Math.max(shardMaxTxCount, state.getLastIndexedTxId());
}
leftTxCount += ((double)shardMaxTxCount)/left.getFirst().getNumberOfShards();
}
double rightTxCount = 0;
for(HashSet states : right.getSecond().values())
{
long shardMaxTxCount = 0;
for(ShardState state : states)
{
shardMaxTxCount = Math.max(shardMaxTxCount, state.getLastIndexedTxId());
}
rightTxCount += ((double)shardMaxTxCount)/right.getFirst().getNumberOfShards();
}
return (int)(rightTxCount - leftTxCount);
}
}
private static String SHARD_STATE_KEY = ".SHARD_STATE";
private AttributeService attributeService;
private SimpleCache shardStateCache;
private SimpleCache shardToGuidCache;
private ConcurrentHashSet knownFlocks = new ConcurrentHashSet();
private Random random = new Random(123);
private boolean purgeOnInit = false;
TransactionService transactionService;
private long shardInstanceTimeoutInSeconds = 300;
private long maxAllowedReplicaTxCountDifference = 1000;
public ShardRegistryImpl()
{
}
public void init()
{
if(purgeOnInit && (transactionService != null))
{
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback