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 3, 2015
1 parent 8c0875f commit 39c7f54
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 61 deletions.
Expand Up @@ -360,7 +360,7 @@ protected GridKernalContextImpl(
this.igfsExecSvc = igfsExecSvc; this.igfsExecSvc = igfsExecSvc;
this.restExecSvc = restExecSvc; this.restExecSvc = restExecSvc;


marshCtx = new MarshallerContextImpl(log); marshCtx = new MarshallerContextImpl();


try { try {
spring = SPRING.create(false); spring = SPRING.create(false);
Expand Down
Expand Up @@ -19,14 +19,11 @@


import org.apache.ignite.*; import org.apache.ignite.*;
import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.processors.cache.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.lang.*;
import org.apache.ignite.marshaller.*; import org.apache.ignite.marshaller.*;
import org.jdk8.backport.*; import org.jdk8.backport.*;


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


/** /**
Expand All @@ -37,7 +34,7 @@ public class MarshallerContextImpl implements MarshallerContext {
private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties"; private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties";


/** */ /** */
private final ConcurrentMap<Integer, IgniteBiTuple<Class, Boolean>> clsById = new ConcurrentHashMap8<>(); private final ConcurrentMap<Integer, String> clsNameById = new ConcurrentHashMap8<>();


/** */ /** */
private final CountDownLatch latch = new CountDownLatch(1); private final CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -46,27 +43,18 @@ public class MarshallerContextImpl implements MarshallerContext {
private volatile GridCacheAdapter<Integer, String> cache; private volatile GridCacheAdapter<Integer, String> cache;


/** /**
* @param log Logger. * Constructor.
*/ */
MarshallerContextImpl(IgniteLogger log) { MarshallerContextImpl() {
try { try {
ClassLoader ldr = getClass().getClassLoader(); ClassLoader ldr = getClass().getClassLoader();


BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(CLS_NAMES_FILE))); BufferedReader rdr = new BufferedReader(new InputStreamReader(ldr.getResourceAsStream(CLS_NAMES_FILE)));


String clsName; String clsName;


while ((clsName = rdr.readLine()) != null) { while ((clsName = rdr.readLine()) != null)
try { clsNameById.put(clsName.hashCode(), clsName);
Class cls = U.forName(clsName, ldr);

clsById.put(cls.getName().hashCode(), F.t(cls, true));
}
catch (ClassNotFoundException | NoClassDefFoundError ignored) {
if (log.isDebugEnabled())
log.debug("Class defined in classnames.properties doesn't exist (ignoring): " + clsName);
}
}
} }
catch (IOException e) { catch (IOException e) {
throw new IllegalStateException("Failed to initialize marshaller context.", e); throw new IllegalStateException("Failed to initialize marshaller context.", e);
Expand All @@ -84,29 +72,9 @@ public void onMarshallerCacheReady(GridKernalContext ctx) {
latch.countDown(); latch.countDown();
} }


/**
* @param ldr Undeployed class loader.
*/
public void onUndeployed(ClassLoader ldr) {
for (Map.Entry<Integer, IgniteBiTuple<Class, Boolean>> e : clsById.entrySet()) {
if (!e.getValue().get2() && ldr.equals(e.getValue().get1().getClassLoader()))
clsById.remove(e.getKey());
}
}

/**
* Clears cached classes.
*/
public void clear() {
for (Map.Entry<Integer, IgniteBiTuple<Class, Boolean>> e : clsById.entrySet()) {
if (!e.getValue().get2())
clsById.remove(e.getKey());
}
}

/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void registerClass(int id, Class cls) { @Override public void registerClass(int id, Class cls) {
if (clsById.putIfAbsent(id, F.t(cls, false)) == null) { if (clsNameById.putIfAbsent(id, cls.getName()) == null) {
try { try {
if (cache == null) if (cache == null)
U.awaitQuiet(latch); U.awaitQuiet(latch);
Expand All @@ -126,14 +94,12 @@ public void clear() {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException { @Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException {
IgniteBiTuple<Class, Boolean> t = clsById.get(id); String clsName = clsNameById.get(id);


if (t == null) { if (clsName == null) {
if (cache == null) if (cache == null)
U.awaitQuiet(latch); U.awaitQuiet(latch);


String clsName;

try { try {
clsName = cache.get(id); clsName = cache.get(id);
} }
Expand All @@ -143,14 +109,12 @@ public void clear() {


assert clsName != null : id; assert clsName != null : id;


Class cls = U.forName(clsName, ldr); String old = clsNameById.putIfAbsent(id, clsName);

IgniteBiTuple<Class, Boolean> old = clsById.putIfAbsent(id, t = F.t(cls, false));


if (old != null) if (old != null)
t = old; clsName = old;
} }


return t.get1(); return U.forName(clsName, ldr);
} }
} }
Expand Up @@ -522,7 +522,6 @@ private void undeploy(ClassLoader ldr) {
if (dep.obsolete()) { if (dep.obsolete()) {
// Resource cleanup. // Resource cleanup.
ctx.resource().onUndeployed(dep); ctx.resource().onUndeployed(dep);
ctx.marshallerContext().onUndeployed(ldr);


// Clear optimized marshaller's cache. If another marshaller is used, this is no-op. // Clear optimized marshaller's cache. If another marshaller is used, this is no-op.
OptimizedMarshaller.onUndeploy(ldr); OptimizedMarshaller.onUndeploy(ldr);
Expand Down
Expand Up @@ -495,7 +495,6 @@ void recordUndeployed() {


ctx.cache().onUndeployed(ldr); ctx.cache().onUndeployed(ldr);
ctx.stream().onUndeployed(ldr); ctx.stream().onUndeployed(ldr);
ctx.marshallerContext().onUndeployed(ldr);


// Clear optimized marshaller's cache. If another marshaller is used, this is no-op. // Clear optimized marshaller's cache. If another marshaller is used, this is no-op.
OptimizedMarshaller.onUndeploy(ldr); OptimizedMarshaller.onUndeploy(ldr);
Expand Down
Expand Up @@ -1258,7 +1258,6 @@ void recordUndeployed(@Nullable UUID leftNodeId) {


ctx.cache().onUndeployed(ldr); ctx.cache().onUndeployed(ldr);
ctx.stream().onUndeployed(ldr); ctx.stream().onUndeployed(ldr);
ctx.marshallerContext().onUndeployed(ldr);


// Clear optimized marshaller's cache. If another marshaller is used, this is no-op. // Clear optimized marshaller's cache. If another marshaller is used, this is no-op.
OptimizedMarshaller.onUndeploy(ldr); OptimizedMarshaller.onUndeploy(ldr);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import org.apache.ignite.internal.util.tostring.*; import org.apache.ignite.internal.util.tostring.*;
import org.apache.ignite.internal.util.typedef.*; import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.internal.util.typedef.internal.*; import org.apache.ignite.internal.util.typedef.internal.*;
import org.apache.ignite.marshaller.*;
import org.apache.ignite.testframework.junits.common.*; import org.apache.ignite.testframework.junits.common.*;
import org.jetbrains.annotations.*; import org.jetbrains.annotations.*;


Expand Down Expand Up @@ -63,14 +64,19 @@ public class GridNioSelfTest extends GridCommonAbstractTest {
/** Count of statistics segments. */ /** Count of statistics segments. */
private static final int STATISTICS_SEGMENTS_CNT = 10; private static final int STATISTICS_SEGMENTS_CNT = 10;


/** {@inheritDoc} */ /** Marshaller. */
@Override protected void afterTestsStopped() throws Exception { private static volatile Marshaller marsh;
getTestResources().stopThreads();
}


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception { @Override protected void beforeTestsStarted() throws Exception {
getTestResources().startThreads(true); getTestResources().startThreads(true);

marsh = getTestResources().getMarshaller();
}

/** {@inheritDoc} */
@Override protected void afterTestsStopped() throws Exception {
getTestResources().stopThreads();
} }


/** /**
Expand Down Expand Up @@ -1115,7 +1121,7 @@ public long average() {
* @throws IgniteCheckedException If failed. * @throws IgniteCheckedException If failed.
*/ */
private <T extends Serializable> byte[] serializeMessage(T msg) throws IgniteCheckedException { private <T extends Serializable> byte[] serializeMessage(T msg) throws IgniteCheckedException {
return getTestResources().getMarshaller().marshal(msg); return marsh.marshal(msg);
} }


/** /**
Expand All @@ -1128,7 +1134,7 @@ private <T extends Serializable> byte[] serializeMessage(T msg) throws IgniteChe
*/ */
@SuppressWarnings({"RedundantTypeArguments"}) @SuppressWarnings({"RedundantTypeArguments"})
private <T> T deserializeMessage(byte[] data) throws IgniteCheckedException { private <T> T deserializeMessage(byte[] data) throws IgniteCheckedException {
return getTestResources().getMarshaller().<T>unmarshal(data, getClass().getClassLoader()); return marsh.<T>unmarshal(data, getClass().getClassLoader());
} }


/** /**
Expand Down
Expand Up @@ -18,9 +18,10 @@
package org.apache.ignite.marshaller; package org.apache.ignite.marshaller;


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


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


/** /**
* Test marshaller context. * Test marshaller context.
Expand All @@ -30,7 +31,7 @@ public class MarshallerContextTestImpl implements MarshallerContext {
private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties"; private static final String CLS_NAMES_FILE = "org/apache/ignite/internal/classnames.properties";


/** */ /** */
private final Map<Integer, Class> map = new HashMap<>(); private final ConcurrentMap<Integer, Class> map = new ConcurrentHashMap8<>();


/** /**
*/ */
Expand Down Expand Up @@ -60,11 +61,19 @@ public MarshallerContextTestImpl() {


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public void registerClass(int id, Class cls) { @Override public void registerClass(int id, Class cls) {
map.put(id, cls); Class old = map.putIfAbsent(id, cls);

if (old != null && !cls.getName().equals(old.getName()))
throw new IllegalStateException("Collision [id=" + id + ", cls1=" + cls.getName() +
", cls2=" + old.getName() + ']');
} }


/** {@inheritDoc} */ /** {@inheritDoc} */
@Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException { @Override public Class className(int id, ClassLoader ldr) throws ClassNotFoundException {
return map.get(id); Class cls = map.get(id);

assert cls != null;

return cls;
} }
} }

0 comments on commit 39c7f54

Please sign in to comment.