com.gs.fw.common.mithra.cache.offheap.MasterCacheUplink Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reladomo Show documentation
Show all versions of reladomo Show documentation
Reladomo is an object-relational mapping framework.
/*
Copyright 2016 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package com.gs.fw.common.mithra.cache.offheap;
import com.gs.fw.common.mithra.MithraObjectPortal;
import com.gs.fw.common.mithra.cache.StringIndex;
import com.gs.fw.common.mithra.util.StringPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class MasterCacheUplink
{
private static final Logger logger = LoggerFactory.getLogger(MasterCacheUplink.class);
private static final AtomicInteger usedThreads = new AtomicInteger();
private static final Long ZERO = 0L;
private final String masterCacheId;
private final MasterCacheService service;
private volatile FastUnsafeOffHeapIntList masterToLocalStringMap = new FastUnsafeOffHeapIntList(10000, true);
private int syncThreads;
private long syncInterval;
private List objectPortals;
private RefreshState nextRefresh;
private final AtomicInteger activePortals = new AtomicInteger(0);
private long lastSuccessfulRefresh = 0;
private volatile boolean paused = false;
private final Object pauseLock = new Object();
public MasterCacheUplink(String masterCacheId, MasterCacheService service)
{
this.service = service;
this.masterCacheId = masterCacheId;
}
public String getMasterCacheId()
{
return masterCacheId;
}
public long getLastSuccessfulRefresh()
{
return lastSuccessfulRefresh;
}
public long getSyncInterval()
{
return syncInterval;
}
public void setSyncInterval(long syncInterval)
{
this.syncInterval = syncInterval;
}
public void pause()
{
this.paused = true;
}
public void unPause()
{
synchronized (this.pauseLock)
{
this.paused = false;
this.pauseLock.notifyAll();
}
}
public int mapMasterStringRefToLocalRef(int masterStringRef)
{
if (masterStringRef >= masterToLocalStringMap.size())
{
refreshStringMap(masterStringRef);
}
if (masterStringRef >= masterToLocalStringMap.size())
{
throwUnknownStringException(masterStringRef);
}
return masterToLocalStringMap.get(masterStringRef);
}
private void throwUnknownStringException(int masterStringRef)
{
throw new RuntimeException("Unknown master cache string ref: "+masterStringRef);
}
private synchronized void refreshStringMap(int masterStringRef)
{
if (masterStringRef >= masterToLocalStringMap.size())
{
syncStrings();
}
}
private synchronized void syncStrings()
{
long start = System.currentTimeMillis();
MasterRetrieveStringResult result = service.retrieveStrings(masterToLocalStringMap.size());
int[] masterRefs = result.getMasterRefs();
String[] masterStrings = result.getMasterStrings();
StringPool.getInstance().ensureCapacity(masterRefs.length);
if (masterRefs.length > this.masterToLocalStringMap.unusedCapacity())
{
FastUnsafeOffHeapIntList newMap = new FastUnsafeOffHeapIntList((int)((masterToLocalStringMap.size() + masterRefs.length)*1.1), true);
newMap.addAll(masterToLocalStringMap);
for(int i=0;i objectPortals)
{
this.objectPortals = objectPortals;
this.activePortals.set(this.objectPortals.size());
MasterRetrieveInitialSyncSizeResult masterRetrieveInitialSyncSizeResult = this.service.retrieveInitialSyncSize();
final Map nameToSizeMap = masterRetrieveInitialSyncSizeResult.getNameToSizeMap();
Collections.sort(this.objectPortals, new Comparator()
{
@Override
public int compare(MithraObjectPortal o1, MithraObjectPortal o2)
{
Long size1 = nameToSizeMap.get(o1.getBusinessClassName());
Long size2 = nameToSizeMap.get(o2.getBusinessClassName());
if (size1 == null) size1 = ZERO;
if (size2 == null) size2 = ZERO;
return (size2 < size1 ? -1 : (size2 == size1 ? 0 : 1));
}
});
RefreshState initialSyncState = new RefreshState(this.objectPortals.size(), System.currentTimeMillis());
this.nextRefresh = initialSyncState;
setUpThreadPool();
initialSyncState.waitForRefreshToFinish();
}
private void setUpThreadPool()
{
for(int i=0;i 0)
{
waitForRefreshToFinish();
}
else
{
// we are the last, we have to setup the next refresh
if (activePortals.get() != 0)
{
long end = System.currentTimeMillis();
long nextRefreshStart = this.startTime + syncInterval;
if (nextRefreshStart < end)
{
nextRefreshStart = end;
}
nextRefresh = new RefreshState(activePortals.get(), nextRefreshStart);
endTime = end;
if (this.failedThrowable == null)
{
lastSuccessfulRefresh = end;
}
this.notifyAll();
logger.info("Cache replication sync for "+masterCacheId+" finished in "+(endTime - startTime)/1000.0+" seconds");
}
else
{
destroy();
}
}
return activePortals.get() == 0;
}
public synchronized void waitForRefreshToFinish()
{
while(endTime == 0)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
//ignore
}
}
}
public synchronized void markRefreshFailed(Throwable t)
{
this.failedThrowable = t;
}
}
}