Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,30 @@ private MainIndexFactory() throws MainIndexException
}
}

private static MainIndexFactory instance = null;
private static volatile MainIndexFactory instance = null;

public static MainIndexFactory Instance() throws MainIndexException
{
if (instance == null)
{
instance = new MainIndexFactory();
Runtime.getRuntime().addShutdownHook(new Thread(()->
synchronized (MainIndexFactory.class)
{
try
if (instance == null)
{
instance.closeAll();
instance = new MainIndexFactory();
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
try
{
instance.closeAll();
} catch (MainIndexException e)
{
logger.error("Failed to close all main index instances.", e);
e.printStackTrace();
}
}));
}
catch (MainIndexException e)
{
logger.error("Failed to close all main index instances.", e);
e.printStackTrace();
}
}));
}
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,31 @@ private SinglePointIndexFactory()
this.singlePointIndexProviders = providersBuilder.build();
}

private static SinglePointIndexFactory instance = null;
private static volatile SinglePointIndexFactory instance = null;

public static SinglePointIndexFactory Instance()
{
if (instance == null)
{
instance = new SinglePointIndexFactory();
Runtime.getRuntime().addShutdownHook(new Thread(()->
synchronized (SinglePointIndexFactory.class)
{
try
{
instance.closeAll();
}
catch (SinglePointIndexException e)
if (instance == null)
{
logger.error("Failed to close all single point index instances.", e);
e.printStackTrace();
instance = new SinglePointIndexFactory();
Runtime.getRuntime().addShutdownHook(new Thread(()->
{
try
{
instance.closeAll();
}
catch (SinglePointIndexException e)
{
logger.error("Failed to close all single point index instances.", e);
e.printStackTrace();
}
}));
}
}));
}
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@

public class IndexFactory
{
private static IndexFactory instance = null;
private static final class InstanceHolder
{
private static final IndexFactory instance = new IndexFactory();
}

public static IndexFactory Instance()
{
if (instance == null)
{
instance = new IndexFactory();
}
return instance;
return InstanceHolder.instance;
}

private final Map<SchemaTableName, SplitsIndex> splitsIndexes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
*/
public class MetadataCache
{
private static final MetadataCache instance = new MetadataCache();
private static final class InstanceHolder
{
private static final MetadataCache instance = new MetadataCache();
}

public static MetadataCache Instance()
{
return instance;
return InstanceHolder.instance;
}

private class TransMetadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -48,7 +49,7 @@ public class MetadataService
{
private static final Logger logger = LogManager.getLogger(MetadataService.class);
private static final MetadataService defaultInstance;
private static final Map<HostAddress, MetadataService> otherInstances = new HashMap<>();
private static final Map<HostAddress, MetadataService> otherInstances = new ConcurrentHashMap<>();

static
{
Expand Down Expand Up @@ -94,7 +95,7 @@ public static MetadataService Instance()
* @param port the port of the metadata server
* @return the created metadata service instance
*/
public static MetadataService CreateInstance(String host, int port)
public static synchronized MetadataService CreateInstance(String host, int port)
{
HostAddress address = HostAddress.fromParts(host, port);
MetadataService metadataService = otherInstances.get(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
*/
public class SchedulerFactory
{
private static SchedulerFactory instance;
private static final class InstanceHolder
{
private static final SchedulerFactory instance = new SchedulerFactory();
}

public static SchedulerFactory Instance()
{
if (instance == null)
{
instance = new SchedulerFactory();
}
return instance;
return InstanceHolder.instance;
}

private final Scheduler scheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,30 @@ private StorageFactory()
this.storageProviders = providersBuilder.build();
}

private static StorageFactory instance = null;
private static volatile StorageFactory instance = null;

public static StorageFactory Instance()
{
if (instance == null)
{
instance = new StorageFactory();
Runtime.getRuntime().addShutdownHook(new Thread(()->
synchronized (StorageFactory.class)
{
try
if (instance == null)
{
instance.closeAll();
} catch (IOException e)
{
logger.error("Failed to close all storage instances.", e);
e.printStackTrace();
instance = new StorageFactory();
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
try
{
instance.closeAll();
} catch (IOException e)
{
logger.error("Failed to close all storage instances.", e);
e.printStackTrace();
}
}));
}
}));
}
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@
*/
public class NoopScheduler implements Scheduler
{
private static Logger logger = LogManager.getLogger(NoopScheduler.class);
private static NoopScheduler instance;
private static final Logger logger = LogManager.getLogger(NoopScheduler.class);
private static final NoopScheduler instance = new NoopScheduler();

public static Scheduler Instance()
{
if (instance == null)
{
instance = new NoopScheduler();
}
// no need to create instance lazily.
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@
*/
public class RateLimitedScheduler extends SortMergeScheduler
{
private static Logger logger = LogManager.getLogger(RateLimitedScheduler.class);
private static RateLimitedScheduler instance;
private static final Logger logger = LogManager.getLogger(RateLimitedScheduler.class);

private static final class InstanceHolder
{
private static final RateLimitedScheduler instance = new RateLimitedScheduler();
}

public static Scheduler Instance()
{
if (instance == null)
{
instance = new RateLimitedScheduler();
}
return instance;
return InstanceHolder.instance;
}

private RateLimiter mbpsRateLimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@
public class SortMergeScheduler implements Scheduler
{
private static final Logger logger = LogManager.getLogger(SortMergeScheduler.class);
private static SortMergeScheduler instance;

private static final class InstanceHolder
{
private static final SortMergeScheduler instance = new SortMergeScheduler();
}
private static int MaxGap;

public static Scheduler Instance()
{
if (instance == null)
{
instance = new SortMergeScheduler();
}
return instance;
return InstanceHolder.instance;
}

protected RetryPolicy retryPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ public static RetinaService Instance()
* @param port the port of the retina server
* @return the created retina service instance
*/
public static RetinaService CreateInstance(String host, int port)
public static synchronized RetinaService CreateInstance(String host, int port)
{
HostAddress address = HostAddress.fromParts(host, port);
return otherInstances.computeIfAbsent(
address,
addr -> new RetinaService(addr.getHostText(), addr.getPort())
address, addr -> new RetinaService(addr.getHostText(), addr.getPort())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.pixelsdb.pixels.daemon.TransProto;

import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -110,6 +111,23 @@ public TransProto.TransContext toProtobuf()
return builder.build();
}

@Override
public int hashCode()
{
return Objects.hash(transId);
}

@Override
public boolean equals(Object obj)
{
if (obj instanceof TransContext)
{
TransContext that = (TransContext) obj;
return this.transId == that.transId;
}
return false;
}

@Override
public int compareTo(TransContext that)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@
*/
public class TransContextCache
{
private static TransContextCache instance;
private static final class InstanceHolder
{
private static final TransContextCache instance = new TransContextCache();
}

public static TransContextCache Instance()
{
if (instance == null)
{
instance = new TransContextCache();
}
return instance;
return InstanceHolder.instance;
}

private final Map<Long, TransContext> transIdToContext = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -47,7 +47,7 @@ public class TransService
{
private static final Logger logger = LogManager.getLogger(TransService.class);
private static final TransService defaultInstance;
private static final Map<HostAddress, TransService> otherInstances = new HashMap<>();
private static final Map<HostAddress, TransService> otherInstances = new ConcurrentHashMap<>();

static
{
Expand Down Expand Up @@ -92,7 +92,7 @@ public static TransService Instance()
* @param port the port of the trans server
* @return the created trans service instance
*/
public static TransService CreateInstance(String host, int port)
public static synchronized TransService CreateInstance(String host, int port)
{
HostAddress address = HostAddress.fromParts(host, port);
TransService transService = otherInstances.get(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
*/
public class InvokerFactory
{
private static final InvokerFactory instance = new InvokerFactory();
private static final class InstanceHolder
{
private static final InvokerFactory instance = new InvokerFactory();
}

public static InvokerFactory Instance()
{
return instance;
return InstanceHolder.instance;
}

/**
Expand Down
Loading