Skip to content

Commit

Permalink
IGNITE-141 - Marshallers refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Kulichenko committed Mar 6, 2015
1 parent cc32cce commit 9d63a88
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 128 deletions.
Expand Up @@ -30,10 +30,10 @@
*/
public class ClassesGenerator {
/** */
private static final String DFLT_BASE_PATH = U.getIgniteHome() + "/modules/core/src/main/java";
private static final String FILE_PATH = "META-INF/classnames.properties";

/** */
private static final String FILE_PATH = "org/apache/ignite/internal/classnames.properties";
private static final String DFLT_BASE_PATH = U.getIgniteHome() + "/modules/core/src/main/resources";

/** */
private static final String HEADER =
Expand All @@ -55,7 +55,7 @@ public class ClassesGenerator {
"#";

/** */
private static final String[] INCLUDED_PACKAGES = {
private static final String[] PACKAGES = {
"org.apache.ignite",
"org.jdk8.backport",
"org.pcollections",
Expand All @@ -69,26 +69,9 @@ public class ClassesGenerator {
public static void main(String[] args) throws Exception {
String basePath = args.length > 0 ? args[0] : DFLT_BASE_PATH;

File file = new File(basePath, FILE_PATH);
ClassesGenerator gen = new ClassesGenerator(basePath, HEADER, PACKAGES);

ClassesGenerator gen = new ClassesGenerator();

write(file, gen.generate());
}

/**
* @param file File.
* @param classes Classes.
* @throws Exception In case of error.
*/
private static void write(File file, Collection<Class> classes) throws Exception {
PrintStream out = new PrintStream(file);

out.println(HEADER);
out.println();

for (Class cls : classes)
out.println(cls.getName());
gen.generate();
}

/** */
Expand All @@ -104,11 +87,30 @@ private static void write(File file, Collection<Class> classes) throws Exception
/** */
private final Collection<String> errs = new ArrayList<>();

/** */
private final String basePath;

/** */
private final String header;

/** */
private final String[] packages;

/**
* @param basePath Base file path.
* @param header Header.
* @param packages Included packages.
*/
public ClassesGenerator(String basePath, String header, String[] packages) {
this.basePath = basePath;
this.header = header;
this.packages = packages;
}

/**
* @throws Exception In case of error.
* @return Classes.
*/
private Collection<Class> generate() throws Exception {
public void generate() throws Exception {
System.out.println("Generating classnames.properties...");

for (URL url : ldr.getURLs())
Expand All @@ -123,7 +125,13 @@ private Collection<Class> generate() throws Exception {
throw new Exception(sb.toString().trim());
}

return classes;
PrintStream out = new PrintStream(new File(basePath, FILE_PATH));

out.println(header);
out.println();

for (Class cls : classes)
out.println(cls.getName());
}

/**
Expand Down Expand Up @@ -183,7 +191,7 @@ private void processClassFile(String path, int prefixLen)

boolean included = false;

for (String pkg : INCLUDED_PACKAGES) {
for (String pkg : packages) {
if (clsName.startsWith(pkg)) {
included = true;

Expand Down
Expand Up @@ -21,17 +21,19 @@
import org.jdk8.backport.*;

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;

/**
* Marshaller context adapter.
*/
public abstract class MarshallerContextAdapter implements MarshallerContext {
/** */
private static final String[] CLS_NAMES_FILES = {
"org/apache/ignite/internal/classnames.properties",
"org/apache/ignite/internal/classnames-jdk.properties"
};
private static final String CLS_NAMES_FILE = "META-INF/classnames.properties";

/** */
private static final String JDK_CLS_NAMES_FILE = "META-INF/classnames-jdk.properties";

/** */
protected final ConcurrentMap<Integer, String> clsNameById = new ConcurrentHashMap8<>();
Expand All @@ -43,23 +45,36 @@ public MarshallerContextAdapter() {
try {
ClassLoader ldr = getClass().getClassLoader();

for (String file : CLS_NAMES_FILES) {
BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(file)));

String line;

while ((line = rdr.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;
Enumeration<URL> urls = ldr.getResources(CLS_NAMES_FILE);

String clsName = line.trim();
while (urls.hasMoreElements())
processResource(urls.nextElement());

clsNameById.put(clsName.hashCode(), clsName);
}
}
processResource(ldr.getResource(JDK_CLS_NAMES_FILE));
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize marshaller context.", e);
}
}

/**
* @param url Resource URL.
* @throws IOException In case of error.
*/
private void processResource(URL url) throws IOException {
try (InputStream in = url.openStream()) {
BufferedReader rdr = new BufferedReader(new InputStreamReader(in));

String line;

while ((line = rdr.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;

String clsName = line.trim();

clsNameById.put(clsName.hashCode(), clsName);
}
}
}
}
Expand Up @@ -47,6 +47,8 @@ public void onMarshallerCacheReady(GridKernalContext ctx) {
/** {@inheritDoc} */
@Override public void registerClass(int id, Class cls) {
if (!clsNameById.containsKey(id)) {
U.debug("REG: " + cls.getName());

try {
if (cache == null)
U.awaitQuiet(latch);
Expand Down Expand Up @@ -89,6 +91,6 @@ public void onMarshallerCacheReady(GridKernalContext ctx) {
}
}

return U.forName(clsName, ldr);
return Class.forName(clsName, false, ldr);
}
}
Expand Up @@ -21,7 +21,6 @@
import org.apache.ignite.internal.*;
import org.apache.ignite.internal.client.marshaller.*;
import org.apache.ignite.internal.processors.rest.client.message.*;
import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.optimized.*;

import java.io.*;
Expand Down Expand Up @@ -112,7 +111,7 @@ private static class ClientMarshallerContext extends MarshallerContextAdapter {

assert clsName != null : id;

return U.forName(clsName, ldr);
return Class.forName(clsName, false, ldr);
}
}
}
Expand Up @@ -266,9 +266,6 @@ public abstract class IgniteUtils {
/** */
static volatile long curTimeMillis = System.currentTimeMillis();

/** Primitive class map. */
private static final Map<String, Class<?>> primitiveMap = new HashMap<>(16, .5f);

/** Boxed class map. */
private static final Map<Class<?>, Class<?>> boxedClsMap = new HashMap<>(16, .5f);

Expand Down Expand Up @@ -398,15 +395,6 @@ else if (archStr.contains("sparc"))
IgniteUtils.javaRtName = javaRtName;
IgniteUtils.javaRtVer = javaRtVer;

primitiveMap.put("byte", byte.class);
primitiveMap.put("short", short.class);
primitiveMap.put("int", int.class);
primitiveMap.put("long", long.class);
primitiveMap.put("float", float.class);
primitiveMap.put("double", double.class);
primitiveMap.put("char", char.class);
primitiveMap.put("boolean", boolean.class);

boxedClsMap.put(byte.class, Byte.class);
boxedClsMap.put(short.class, Short.class);
boxedClsMap.put(int.class, Integer.class);
Expand Down Expand Up @@ -7897,24 +7885,6 @@ public static int safeAbs(int i) {
return boxed != null ? boxed : cls;
}

/**
* Gets class for provided name. Accepts primitive types names.
*
* @param clsName Class name.
* @param ldr Class loader.
* @return Class.
* @throws ClassNotFoundException If class not found.
*/
public static Class<?> forName(@Nullable String clsName, @Nullable ClassLoader ldr)
throws ClassNotFoundException {
if (clsName == null)
return null;

Class<?> cls = primitiveMap.get(clsName);

return cls != null ? cls : Class.forName(clsName, true, ldr);
}

/**
* Applies a supplemental hash function to a given hashCode, which
* defends against poor quality hash functions. This is critical
Expand Down
Expand Up @@ -877,7 +877,7 @@ Object read(OptimizedObjectInputStream in) throws ClassNotFoundException, IOExce
return in.readBooleanArray();

case TYPE_OBJ_ARR:
return in.readArray(U.forName(in.readUTF(), in.classLoader()));
return in.readArray(Class.forName(in.readUTF(), false, in.classLoader()));

case TYPE_STR:
return in.readString();
Expand Down
Expand Up @@ -17,52 +17,33 @@

package org.apache.ignite.marshaller;

import org.apache.ignite.internal.*;
import org.jdk8.backport.*;

import java.io.*;
import java.util.*;
import java.util.concurrent.*;

/**
* Test marshaller context.
*/
public class MarshallerContextTestImpl implements MarshallerContext {
/** */
private static final String[] CLS_NAMES_FILES = {
"org/apache/ignite/internal/classnames.properties",
"org/apache/ignite/internal/classnames-jdk.properties"
};

public class MarshallerContextTestImpl extends MarshallerContextAdapter {
/** */
private final ConcurrentMap<Integer, Class> map = new ConcurrentHashMap8<>();

/**
*/
public MarshallerContextTestImpl() {
try {
ClassLoader ldr = getClass().getClassLoader();

for (String file : CLS_NAMES_FILES) {
BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(file)));

String line;
super();

while ((line = rdr.readLine()) != null) {
if (line.isEmpty() || line.startsWith("#"))
continue;
ClassLoader ldr = getClass().getClassLoader();

try {
Class cls = Class.forName(line.trim(), false, ldr);

map.put(cls.getName().hashCode(), cls);
}
catch (ClassNotFoundException | NoClassDefFoundError ignored) {
// No-op.
}
}
for (Map.Entry<Integer, String> e : clsNameById.entrySet()) {
try {
map.put(e.getKey(), Class.forName(e.getValue(), true, ldr));
}
catch (ClassNotFoundException | NoClassDefFoundError ignored) {
// No-op.
}
}
catch (IOException e) {
throw new IllegalStateException("Failed to initialize marshaller context.", e);
}
}

Expand Down
24 changes: 2 additions & 22 deletions modules/yardstick/config/benchmark-multicast.properties
Expand Up @@ -36,7 +36,7 @@ JVM_OPTS=${JVM_OPTS}" \
-XX:CMSInitiatingOccupancyFraction=60 \
"
#Ignite version
ver="RELEASE-"
ver="141-"

# List of default probes.
# Add DStatProbe or VmStatProbe if your OS supports it (e.g. if running on Linux).
Expand Down Expand Up @@ -65,25 +65,5 @@ nodesNum=$((`echo ${SERVER_HOSTS} | tr ',' '\n' | wc -l` + `echo ${DRIVER_HOSTS}
# Run configuration which contains all benchmarks.
# Note that each benchmark is set to run for 300 seconds (5 mins) with warm-up set to 60 seconds (1 minute).
CONFIGS="\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutBenchmark -sn IgniteNode -ds ${ver}atomic-put-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutGetBenchmark -sn IgniteNode -ds ${ver}atomic-put-get-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutTxBenchmark -sn IgniteNode -ds ${ver}tx-put-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutGetTxBenchmark -sn IgniteNode -ds ${ver}tx-put-get-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryBenchmark -sn IgniteNode -ds ${ver}sql-query-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryJoinBenchmark -sn IgniteNode -ds ${ver}sql-query-join-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryPutBenchmark -sn IgniteNode -ds ${ver}sql-query-put-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -j 10 -dn IgniteAffinityCallBenchmark -sn IgniteNode -ds ${ver}affcall-compute-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -j 10 -dn IgniteApplyBenchmark -sn IgniteNode -ds ${ver}apply-compute-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -j 10 -dn IgniteBroadcastBenchmark -sn IgniteNode -ds ${ver}broad-compute-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -j 10 -dn IgniteExecuteBenchmark -sn IgniteNode -ds ${ver}exec-compute-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -j 10 -dn IgniteRunBenchmark -sn IgniteNode -ds ${ver}run-compute-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutGetOffHeapBenchmark -sn IgniteNode -ds ${ver}atomic-put-get-offheap-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutGetOffHeapValuesBenchmark -sn IgniteNode -ds ${ver}atomic-put-get-offheap-val-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutOffHeapBenchmark -sn IgniteNode -ds ${ver}atomic-put-offheap-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutOffHeapValuesBenchmark -sn IgniteNode -ds ${ver}atomic-put-offheap-val-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutTxOffHeapBenchmark -sn IgniteNode -ds ${ver}tx-put-offheap-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutTxOffHeapValuesBenchmark -sn IgniteNode -ds ${ver}tx-put-offheap-val-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryOffHeapBenchmark -sn IgniteNode -ds ${ver}sql-query-offheap-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryJoinOffHeapBenchmark -sn IgniteNode -ds ${ver}sql-query-join-offheap-1-backup,\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgniteSqlQueryPutOffHeapBenchmark -sn IgniteNode -ds ${ver}sql-query-put-offheap-1-backup\
-cfg ${SCRIPT_DIR}/../config/ignite-multicast-config.xml -nn ${nodesNum} -b 1 -w 60 -d 300 -t 64 -sm PRIMARY_SYNC -dn IgnitePutGetBenchmark -sn IgniteNode -ds ${ver}atomic-put-get-1-backup\
"

0 comments on commit 9d63a88

Please sign in to comment.