Skip to content

Commit

Permalink
GG-20488 Check deployment from cache before to load it from local or …
Browse files Browse the repository at this point in the history
…version storage
  • Loading branch information
vldpyatkov committed Aug 14, 2019
1 parent 9fb05f1 commit cc7e085
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public Class<?> existingDeployedClass(String clsName) {

if (cls == null) {
try {
cls = Class.forName(clsName, true, clsLdr);
cls = U.forName(clsName, clsLdr);

Class<?> cur = clss.putIfAbsent(clsName, cls);

Expand All @@ -477,7 +477,7 @@ public Class<?> existingDeployedClass(String clsName) {
return cls;
else if (!a.equals(clsName)) {
try {
cls = Class.forName(a, true, clsLdr);
cls = U.forName(a, clsLdr);
}
catch (ClassNotFoundException ignored0) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class GridDeploymentLocalStore extends GridDeploymentStoreAdapter {
// Check that class can be loaded.
String clsName = meta.className();

Class<?> cls = Class.forName(clsName != null ? clsName : alias, true, ldr);
Class<?> cls = U.forName(clsName != null ? clsName : alias, ldr);

spi.register(ldr, cls);

Expand Down Expand Up @@ -226,6 +226,11 @@ class GridDeploymentLocalStore extends GridDeploymentStoreAdapter {
return dep;
}

/** {@inheritDoc} */
@Override public GridDeployment searchDeploymentCache(GridDeploymentMetadata meta) {
return deployment(meta.alias());
}

/**
* @param alias Class alias.
* @return Deployment.
Expand Down Expand Up @@ -444,7 +449,7 @@ private void recordDeployFailed(Class<?> cls, ClassLoader clsLdr, boolean record

evt.message(msg);
evt.node(ctx.discovery().localNode());
evt.type(isTask(cls) ? EVT_CLASS_DEPLOY_FAILED : EVT_TASK_DEPLOY_FAILED);
evt.type(isTask ? EVT_CLASS_DEPLOY_FAILED : EVT_TASK_DEPLOY_FAILED);
evt.alias(taskName);

ctx.event().record(evt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ else if (locDep != null) {
}
}

GridDeployment dep = verStore.searchDeploymentCache(meta);

if (dep != null)
return dep;

if (reuse) {
GridDeployment locDep = locStore.getDeployment(meta);

Expand Down Expand Up @@ -495,7 +500,12 @@ else if (locDep != null) {
// Private or Isolated mode.
meta.record(false);

GridDeployment dep = locStore.getDeployment(meta);
GridDeployment dep = ldrStore.searchDeploymentCache(meta);

if (dep != null)
return dep;

dep = locStore.getDeployment(meta);

if (sndNodeId.equals(ctx.localNodeId())) {
if (dep == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public class GridDeploymentPerLoaderStore extends GridDeploymentStoreAdapter {
IsolatedDeployment dep;

synchronized (mux) {
dep = cache.get(meta.classLoaderId());
dep = (IsolatedDeployment)searchDeploymentCache(meta);

if (dep == null) {
long undeployTimeout = 0;
Expand Down Expand Up @@ -330,6 +330,11 @@ else if (d.sequenceNumber() > meta.sequenceNumber()) {
return dep;
}

/** {@inheritDoc} */
@Override public GridDeployment searchDeploymentCache(GridDeploymentMetadata meta) {
return cache.get(meta.classLoaderId());
}

/** {@inheritDoc} */
@Override public void addParticipants(Map<UUID, IgniteUuid> allParticipants,
Map<UUID, IgniteUuid> addedParticipants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ else if (log.isDebugEnabled())
}
}

/** {@inheritDoc} */
@Override public GridDeployment searchDeploymentCache(GridDeploymentMetadata meta) {
List<SharedDeployment> deps = null;

synchronized (mux) {
deps = cache.get(meta.userVersion());
}

if (deps != null) {
assert !deps.isEmpty();

for (SharedDeployment d : deps) {
if (d.hasParticipant(meta.senderNodeId(), meta.classLoaderId()))
return d;
}
}

return null;
}

/** {@inheritDoc} */
@Override @Nullable public GridDeployment getDeployment(GridDeploymentMetadata meta) {
assert meta != null;
Expand Down Expand Up @@ -356,22 +376,14 @@ else if (ctx.discovery().node(meta.senderNodeId()) == null) {
return null;
}

List<SharedDeployment> deps = cache.get(meta.userVersion());

if (deps != null) {
assert !deps.isEmpty();
dep = (SharedDeployment)searchDeploymentCache(meta);

for (SharedDeployment d : deps) {
if (d.hasParticipant(meta.senderNodeId(), meta.classLoaderId()) ||
meta.senderNodeId().equals(ctx.localNodeId())) {
// Done.
dep = d;
if (dep == null) {
List<SharedDeployment> deps = cache.get(meta.userVersion());

break;
}
}
if (deps != null) {
assert !deps.isEmpty();

if (dep == null) {
checkRedeploy(meta);

// Find existing deployments that need to be checked
Expand Down Expand Up @@ -413,12 +425,12 @@ else if (ctx.discovery().node(meta.senderNodeId()) == null) {
deps.add(dep);
}
}
}
else {
checkRedeploy(meta);
else {
checkRedeploy(meta);

// Create peer class loader.
dep = createNewDeployment(meta, true);
// Create peer class loader.
dep = createNewDeployment(meta, true);
}
}
}

Expand Down Expand Up @@ -1196,8 +1208,6 @@ boolean hasParticipant(UUID nodeId, IgniteUuid ldrId) {
assert nodeId != null;
assert ldrId != null;

assert Thread.holdsLock(mux);

return classLoader().hasRegisteredNode(nodeId, ldrId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public interface GridDeploymentStore {
*/
@Nullable public GridDeployment getDeployment(GridDeploymentMetadata meta);

/**
* @param meta Deployment meatdata.
* @return Grid deployment instance if it was finded in cache, {@code null} otherwise.
*/
@Nullable public GridDeployment searchDeploymentCache(GridDeploymentMetadata meta);

/**
* Gets class loader based on ID.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2019 GridGain Systems, Inc. and Contributors.
*
* Licensed under the GridGain Community Edition License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
*
* 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.ignite.p2p;

import java.net.URL;
import java.net.URLClassLoader;
import org.apache.ignite.Ignite;
import org.apache.ignite.configuration.DeploymentMode;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.lang.IgniteCallable;
import org.apache.ignite.testframework.config.GridTestProperties;
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.apache.ignite.testframework.junits.common.GridCommonTest;
import org.junit.Test;

/**
* Checks count of tries to load class directly from class loader.
*/
@GridCommonTest(group = "P2P")
public class GridP2PCountTiesLoadClassDirectlyFromClassLoaderTest extends GridCommonAbstractTest {
/** P2P class path property. */
public static final String CLS_PATH_PROPERTY = "p2p.uri.cls";
/** Compute task name. */
private static String COMPUTE_TASK_NAME = "org.apache.ignite.tests.p2p.compute.ExternalCallable";

/** Deployment mode. */
private DeploymentMode depMode;

/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
return super.getConfiguration(igniteInstanceName)
.setDeploymentMode(depMode)
.setPeerClassLoadingEnabled(true);
}

/**
* @throws Exception if error occur.
*/
public void executeP2PTask(DeploymentMode depMode) throws Exception {
try {
CountTriesClassLoader testCountLdr = new CountTriesClassLoader(Thread.currentThread()
.getContextClassLoader());

this.depMode = depMode;

Thread.currentThread().setContextClassLoader(testCountLdr);

String path = GridTestProperties.getProperty(CLS_PATH_PROPERTY);

ClassLoader urlClsLdr = new URLClassLoader(new URL[] {new URL(path)}, testCountLdr);

Ignite ignite = startGrids(2);

ignite.compute(ignite.cluster().forRemotes()).call((IgniteCallable)urlClsLdr.loadClass(COMPUTE_TASK_NAME)
.newInstance());

int count = testCountLdr.count;

ignite.compute(ignite.cluster().forRemotes()).call((IgniteCallable)urlClsLdr.loadClass(COMPUTE_TASK_NAME)
.newInstance());
ignite.compute(ignite.cluster().forRemotes()).call((IgniteCallable)urlClsLdr.loadClass(COMPUTE_TASK_NAME)
.newInstance());
ignite.compute(ignite.cluster().forRemotes()).call((IgniteCallable)urlClsLdr.loadClass(COMPUTE_TASK_NAME)
.newInstance());

assertEquals(count, testCountLdr.count);
}
finally {
stopAllGrids();
}
}

/**
* @throws Exception if error occur.
*/
@Test
public void testPrivateMode() throws Exception {
executeP2PTask(DeploymentMode.PRIVATE);
}

/**
* @throws Exception if error occur.
*/
@Test
public void testIsolatedMode() throws Exception {
executeP2PTask(DeploymentMode.ISOLATED);
}

/**
* @throws Exception if error occur.
*/
@Test
public void testContinuousMode() throws Exception {
executeP2PTask(DeploymentMode.CONTINUOUS);
}

/**
* @throws Exception if error occur.
*/
@Test
public void testSharedMode() throws Exception {
executeP2PTask(DeploymentMode.SHARED);
}

/**
* Test count class loader.
*/
private static class CountTriesClassLoader extends ClassLoader {
/** Count of tries. */
int count = 0;

/**
* @param parent Parent class loader.
*/
public CountTriesClassLoader(ClassLoader parent) {
super(parent);
}

/** {@inheritDoc} */
@Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (COMPUTE_TASK_NAME.equals(name))
U.dumpStack(log, "Try to load class: " + name + " " + ++count);

return super.loadClass(name, resolve);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.ignite.p2p.GridP2PDoubleDeploymentSelfTest;
import org.apache.ignite.p2p.GridP2PHotRedeploymentSelfTest;
import org.apache.ignite.p2p.GridP2PJobClassLoaderSelfTest;
import org.apache.ignite.p2p.GridP2PCountTiesLoadClassDirectlyFromClassLoaderTest;
import org.apache.ignite.p2p.GridP2PLocalDeploymentSelfTest;
import org.apache.ignite.p2p.GridP2PMissedResourceCacheSizeSelfTest;
import org.apache.ignite.p2p.GridP2PNodeLeftSelfTest;
Expand Down Expand Up @@ -65,6 +66,7 @@
P2PScanQueryUndeployTest.class,
GridDeploymentMessageCountSelfTest.class,
GridP2PComputeWithNestedEntryProcessorTest.class,
GridP2PCountTiesLoadClassDirectlyFromClassLoaderTest.class
})
public class IgniteP2PSelfTestSuite {
}

0 comments on commit cc7e085

Please sign in to comment.