Skip to content

Commit

Permalink
KYLIN-1816 More than one base KylinConfig exist in spring JVM
Browse files Browse the repository at this point in the history
  • Loading branch information
binmahone committed Jun 23, 2016
1 parent b514ae1 commit 1b0ece4
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 45 deletions.
78 changes: 51 additions & 27 deletions core-common/src/main/java/org/apache/kylin/common/KylinConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,28 @@ public class KylinConfig extends KylinConfigBase {
private static KylinConfig ENV_INSTANCE = null;

public static KylinConfig getInstanceFromEnv() {
if (ENV_INSTANCE == null) {
try {
KylinConfig config = loadKylinConfig();
ENV_INSTANCE = config;
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Failed to find KylinConfig ", e);
synchronized (KylinConfig.class) {
if (ENV_INSTANCE == null) {
try {
KylinConfig config = loadKylinConfig();
logger.info("Initialized a new KylinConfig from getInstanceFromEnv : " + System.identityHashCode(config));
ENV_INSTANCE = config;
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Failed to find KylinConfig ", e);
}
}
return ENV_INSTANCE;
}
return ENV_INSTANCE;
}

//Only used in test cases!!!
public static void destroyInstance() {
logger.info("Destory KylinConfig");
dumpStackTrace();
ENV_INSTANCE = null;
}

public static enum UriType {
public enum UriType {
PROPERTIES_FILE, REST_ADDR, LOCAL_FOLDER
}

Expand Down Expand Up @@ -150,12 +156,15 @@ public static KylinConfig createInstanceFromUri(String uri) {
}

public static void setKylinConfigFromInputStream(InputStream is) {
if (ENV_INSTANCE == null) {
try {
KylinConfig config = createKylinConfigFromInputStream(is);
ENV_INSTANCE = config;
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Failed to find KylinConfig ", e);
synchronized (KylinConfig.class) {
if (ENV_INSTANCE == null) {
try {
KylinConfig config = createKylinConfigFromInputStream(is);
logger.info("Resetting ENV_INSTANCE by a input stream: " + System.identityHashCode(config));
ENV_INSTANCE = config;
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Failed to find KylinConfig ", e);
}
}
}
}
Expand Down Expand Up @@ -254,11 +263,11 @@ private static KylinConfig loadKylinConfig() {

return config;
}

public static void setSandboxEnvIfPossible() {
File dir1 = new File("../examples/test_case_data/sandbox");
File dir2 = new File("../../kylin/examples/test_case_data/sandbox");

if (dir1.exists()) {
logger.info("Setting sandbox env, KYLIN_CONF=" + dir1.getAbsolutePath());
ClassUtil.addClasspath(dir1.getAbsolutePath());
Expand All @@ -272,11 +281,13 @@ public static void setSandboxEnvIfPossible() {

// ============================================================================

public KylinConfig() {
private KylinConfig() {
super();
logger.info("New KylinConfig " + System.identityHashCode(this));
KylinConfig.dumpStackTrace();
}

public KylinConfig(Properties props) {
protected KylinConfig(Properties props) {
super(props);
}

Expand Down Expand Up @@ -305,22 +316,19 @@ private void list(PrintWriter out) {
}
}

private KylinConfig base() {
if (this instanceof KylinConfigExt)
return ((KylinConfigExt) this).base;
else
return this;
public KylinConfig base() {
return this;
}

private int superHashCode() {
return super.hashCode();
}

@Override
public int hashCode() {
return base().superHashCode();
}

@Override
public boolean equals(Object another) {
if (!(another instanceof KylinConfig))
Expand All @@ -329,7 +337,6 @@ public boolean equals(Object another) {
return this.base() == ((KylinConfig) another).base();
}


public static void writeOverrideProperties(Properties properties) throws IOException {
File propFile = getKylinProperties();
File overrideFile = new File(propFile.getParentFile(), propFile.getName() + ".override");
Expand Down Expand Up @@ -362,4 +369,21 @@ public static void writeOverrideProperties(Properties properties) throws IOExcep
}

}

private static void dumpStackTrace() {
Thread t = Thread.currentThread();
int maxStackTraceDepth = 20;
int current = 0;

StackTraceElement[] stackTrace = t.getStackTrace();
StringBuilder buf = new StringBuilder("");
buf.append("\n");
for (StackTraceElement e : stackTrace) {
if (++current > maxStackTraceDepth) {
break;
}
buf.append("\t").append("at ").append(e.toString()).append("\n");
}
logger.info(buf.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public Properties getAllProperties() {
return result;
}

@Override
public KylinConfig base() {
return this.base;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static Broadcaster getInstance(KylinConfig config) {
r = new Broadcaster(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one cubemanager singleton exist");
logger.warn("More than one singleton exist");
}
return r;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.kylin.common.util;

import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;

public class SerializeToByteBuffer {

public interface IWriter {
void write(ByteBuffer byteBuffer) throws BufferOverflowException;
}

public static ByteBuffer retrySerialize(IWriter writer) {
int bufferSize = BytesSerializer.SERIALIZE_BUFFER_SIZE;

while (true) {
try {
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
writer.write(byteBuffer);
return byteBuffer;
} catch (BufferOverflowException boe) {
System.out.println("Buffer size cannot hold the raw scans, resizing to 4 times : " + bufferSize);
bufferSize *= 4;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ public static CubeManager getInstance(KylinConfig config) {
r = new CubeManager(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one cubemanager singleton exist");
logger.warn("More than one singleton exist");
for (KylinConfig kylinConfig : CACHE.keySet()) {
logger.warn("type: " + kylinConfig.getClass() + " reference: " + System.identityHashCode(kylinConfig.base()));
}
}
return r;
} catch (IOException e) {
Expand Down Expand Up @@ -149,7 +152,7 @@ public CubeInstance getCubeByUuid(String uuid) {
}
return null;
}

/**
* Get related Cubes by cubedesc name. By default, the desc name will be
* translated into upper case.
Expand Down Expand Up @@ -411,7 +414,7 @@ public CubeSegment appendSegment(CubeInstance cube, long startDate, long endDate

public CubeSegment appendSegment(CubeInstance cube, long startDate, long endDate, long startOffset, long endOffset, boolean strictChecking) throws IOException {

if(strictChecking)
if (strictChecking)
checkNoBuildingSegment(cube);

if (cube.getDescriptor().getModel().getPartitionDesc().isPartitioned()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.kylin.gridtable;

import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
Expand All @@ -29,6 +30,7 @@
import org.apache.kylin.common.util.BytesSerializer;
import org.apache.kylin.common.util.BytesUtil;
import org.apache.kylin.common.util.ImmutableBitSet;
import org.apache.kylin.common.util.SerializeToByteBuffer;
import org.apache.kylin.metadata.filter.TupleFilter;
import org.apache.kylin.metadata.model.TblColRef;

Expand Down Expand Up @@ -270,6 +272,16 @@ public String toString() {
return "GTScanRequest [range=" + ranges + ", columns=" + columns + ", filterPushDown=" + filterPushDown + ", aggrGroupBy=" + aggrGroupBy + ", aggrMetrics=" + aggrMetrics + ", aggrMetricsFuncs=" + Arrays.toString(aggrMetricsFuncs) + "]";
}

public byte[] toByteArray() {
ByteBuffer byteBuffer = SerializeToByteBuffer.retrySerialize(new SerializeToByteBuffer.IWriter() {
@Override
public void write(ByteBuffer byteBuffer) throws BufferOverflowException {
GTScanRequest.serializer.serialize(GTScanRequest.this, byteBuffer);
}
});
return Arrays.copyOf(byteBuffer.array(), byteBuffer.position());
}

public static final BytesSerializer<GTScanRequest> serializer = new BytesSerializer<GTScanRequest>() {
@Override
public void serialize(GTScanRequest value, ByteBuffer out) {
Expand Down Expand Up @@ -340,7 +352,6 @@ private GTRecord deserializeGTRecord(ByteBuffer in, GTInfo sInfo) {
}
return new GTRecord(sInfo, sCols);
}


};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static DictionaryManager getInstance(KylinConfig config) {
if (r == null) {
r = new DictionaryManager(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one singleton exist");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.NavigableSet;
import java.util.NavigableSet;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.kylin.common.KylinConfig;
Expand All @@ -45,12 +45,15 @@ public class SnapshotManager {
public static SnapshotManager getInstance(KylinConfig config) {
SnapshotManager r = SERVICE_CACHE.get(config);
if (r == null) {
synchronized (SnapshotManager.class) {
r = SERVICE_CACHE.get(config);
if (r == null) {
r = new SnapshotManager(config);
SERVICE_CACHE.put(config, r);
}
synchronized (SnapshotManager.class) {
r = SERVICE_CACHE.get(config);
if (r == null) {
r = new SnapshotManager(config);
SERVICE_CACHE.put(config, r);
if (SERVICE_CACHE.size() > 1) {
logger.warn("More than one singleton exist");
}
}
}
}
return r;
Expand Down Expand Up @@ -140,7 +143,7 @@ public SnapshotTable trySaveNewSnapshot(SnapshotTable snapshotTable) throws IOEx
private String checkDupByInfo(SnapshotTable snapshot) throws IOException {
ResourceStore store = MetadataManager.getInstance(this.config).getStore();
String resourceDir = snapshot.getResourceDir();
NavigableSet<String> existings = store.listResources(resourceDir);
NavigableSet<String> existings = store.listResources(resourceDir);
if (existings == null)
return null;

Expand All @@ -158,7 +161,7 @@ private String checkDupByInfo(SnapshotTable snapshot) throws IOException {
private String checkDupByContent(SnapshotTable snapshot) throws IOException {
ResourceStore store = MetadataManager.getInstance(this.config).getStore();
String resourceDir = snapshot.getResourceDir();
NavigableSet<String> existings = store.listResources(resourceDir);
NavigableSet<String> existings = store.listResources(resourceDir);
if (existings == null)
return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public static ExecutableDao getInstance(KylinConfig config) {
if (r == null) {
r = new ExecutableDao(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one singleton exist");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public final ExecuteResult execute(ExecutableContext executableContext) throws E
try {
result = doWork(executableContext);
} catch (Throwable e) {
logger.error("error running Executable", e);
logger.error("error running Executable: " + this.toString());
exception = e;
}
retry++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static ExecutableManager getInstance(KylinConfig config) {
if (r == null) {
r = new ExecutableManager(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one singleton exist");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public static RealizationRegistry getInstance(KylinConfig config) {
try {
r = new RealizationRegistry(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one singleton exist");
}
return r;
} catch (IOException e) {
throw new IllegalStateException("Failed to init CubeManager from " + config, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static HybridManager getInstance(KylinConfig config) {
r = new HybridManager(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one Hybrid Manager singleton exist");
logger.warn("More than one singleton exist");
}
return r;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static StreamingManager getInstance(KylinConfig config) {
r = new StreamingManager(config);
CACHE.put(config, r);
if (CACHE.size() > 1) {
logger.warn("More than one streamingManager singleton exist");
logger.warn("More than one singleton exist");
}
return r;
} catch (IOException e) {
Expand Down
Loading

0 comments on commit 1b0ece4

Please sign in to comment.