Skip to content

Commit

Permalink
- pushing exmpty context in ClusteredStore to prevent association of …
Browse files Browse the repository at this point in the history
…Hazelcast maps to tenant applications (#5188)

that may be later undeployed and would leave maps orphan, which in turn will lead
  to Hazelcast blocking calls to those maps
- minor cleanup
  • Loading branch information
lprimak authored Apr 8, 2021
1 parent 3a7dec5 commit c2444a8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ private boolean isLoaded(String componentId, ComponentInvocation invocation) {
JndiNameEnvironment env = invocation != null ? ((JndiNameEnvironment) invocation.getJNDIEnvironment())
: compEnvMgr.getJndiNameEnvironment(componentId);
if (env != null) {
ApplicationInfo appInfo = appRegistry.get(DOLUtils.getApplicationFromEnv(env).getRegistrationName());
ApplicationInfo appInfo = null;
try {
appInfo = appRegistry.get(DOLUtils.getApplicationFromEnv(env).getRegistrationName());
} catch (IllegalArgumentException e) {
// empty environment, not associated with any app
}
if (appInfo != null) {
// Check if deployed vs. Payara internal application
Collection<ModuleInfo> modules = appInfo.getModuleInfos();
Expand Down Expand Up @@ -241,11 +246,16 @@ public Context pushContext() {
}

private Context pushEmptyContext() {
JndiNameEnvironment env = (JndiNameEnvironment)Proxy.newProxyInstance(Utility.getClassLoader(),
new Class[] { JndiNameEnvironment.class }, (proxy, method, args) -> null);
ComponentInvocation newInvocation = createInvocation(env, "___EMPTY___");
invocationManager.preInvoke(newInvocation);
return new ContextImpl.Context(newInvocation, invocationManager, compEnvMgr, Utility.getClassLoader());
try {
JndiNameEnvironment env = (JndiNameEnvironment) Proxy.newProxyInstance(Utility.getClassLoader(),
new Class<?>[]{JndiNameEnvironment.class}, (proxy, method, args) -> null);
ComponentInvocation newInvocation = createInvocation(env, "___EMPTY___");
invocationManager.preInvoke(newInvocation);
return new ContextImpl.Context(newInvocation, invocationManager, compEnvMgr, Utility.getClassLoader());
} catch (IllegalArgumentException e) {
// Outside of HK2 / Felix context
return new ContextImpl.ClassLoaderContext(null, false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) [2016-2020] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2016-2021] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -120,13 +120,13 @@ public Closeable setTenant() {
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeObject(contextInstance);
out.writeUTF(moduleName);
out.writeString(moduleName);
}

@Override
public void readData(ObjectDataInput in) throws IOException {
contextInstance = in.readObject();
moduleName = in.readUTF();
moduleName = in.readString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2016-2020 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2016-2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -64,6 +64,8 @@
import java.util.Set;
import java.util.UUID;
import java.util.logging.Logger;
import org.glassfish.internal.api.JavaEEContextUtil;
import org.glassfish.internal.api.JavaEEContextUtil.Context;

/**
* Very Simple Store interface to Hazelcast
Expand All @@ -73,13 +75,16 @@
@RunLevel(StartupRunLevel.VAL)
public class ClusteredStore implements EventListener, MonitoringDataSource {
private static final Logger logger = Logger.getLogger(ClusteredStore.class.getCanonicalName());

@Inject
private HazelcastCore hzCore;

@Inject
private Events events;


@Inject
private JavaEEContextUtil ctxUtil;

@PostConstruct
public void postConstruct() {
events.register(this);
Expand All @@ -88,18 +93,20 @@ public void postConstruct() {
@Override
public void collect(MonitoringDataCollector collector) {
if (hzCore.isEnabled()) {
HazelcastInstance hz = hzCore.getInstance();
for (DistributedObject obj : hz.getDistributedObjects()) {
if (MapService.SERVICE_NAME.equals(obj.getServiceName())) {
MapConfig config = hz.getConfig().getMapConfig(obj.getName());
if (config.isStatisticsEnabled()) {
IMap<Object, Object> map = hz.getMap(obj.getName());
if (map != null) {
LocalMapStats stats = map.getLocalMapStats();
collector.in("map").group(map.getName())
.collect("GetCount", stats.getGetOperationCount())
.collect("PutCount", stats.getPutOperationCount())
.collect("EntryCount", stats.getOwnedEntryCount());
try (Context ctx = ctxUtil.empty().pushContext()) {
HazelcastInstance hz = hzCore.getInstance();
for (DistributedObject obj : hz.getDistributedObjects()) {
if (MapService.SERVICE_NAME.equals(obj.getServiceName())) {
MapConfig config = hz.getConfig().getMapConfig(obj.getName());
if (config.isStatisticsEnabled()) {
IMap<Object, Object> map = hz.getMap(obj.getName());
if (map != null) {
LocalMapStats stats = map.getLocalMapStats();
collector.in("map").group(map.getName())
.collect("GetCount", stats.getGetOperationCount())
.collect("PutCount", stats.getPutOperationCount())
.collect("EntryCount", stats.getOwnedEntryCount());
}
}
}
}
Expand All @@ -110,15 +117,15 @@ public void collect(MonitoringDataCollector collector) {
public UUID getInstanceId() {
return hzCore.getUUID();
}

/**
* Returns true if Hazelcast is enabled
* @return
* @return
*/
public boolean isEnabled() {
return hzCore.isEnabled();
}

/**
* Stores a value in Hazelcast
* @param storeName The name of the store to put the value into.
Expand All @@ -130,16 +137,18 @@ public boolean isEnabled() {
public boolean set(String storeName, Serializable key, Serializable value) {
boolean result = false;
if (isEnabled()) {
if (value != null && hzCore.isDatagridEncryptionEnabled()) {
value = new PayaraHazelcastEncryptedValueHolder(HazelcastSymmetricEncryptor.encode(
HazelcastSymmetricEncryptor.objectToByteArray(value)));
try (Context ctx = ctxUtil.empty().pushContext()) {
if (value != null && hzCore.isDatagridEncryptionEnabled()) {
value = new PayaraHazelcastEncryptedValueHolder(HazelcastSymmetricEncryptor.encode(
HazelcastSymmetricEncryptor.objectToByteArray(value)));
}
hzCore.getInstance().getMap(storeName).set(key, value);
result = true;
}
hzCore.getInstance().getMap(storeName).set(key, value);
result = true;
}
return result;
}

/**
* Removes a key/value pair of a Hazelcast store.
* The store will be created if it does not already exist.
Expand All @@ -150,51 +159,57 @@ public boolean set(String storeName, Serializable key, Serializable value) {
public boolean remove(String storeName, Serializable key) {
boolean result = false;
if (isEnabled()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
Object value = map.remove(key);
result = true;
try (Context ctx = ctxUtil.empty().pushContext()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
Object value = map.remove(key);
result = true;
}
}
}
return result;
}

/**
* Checks to see if a a key already exists in Hazelcast.
* The store will be created if it does not already exist.
* @param storeName
* @param key
* @return
* @return
*/
public boolean containsKey(String storeName, Serializable key) {
boolean result = false;
if (isEnabled()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
result = map.containsKey(key);
try (Context ctx = ctxUtil.empty().pushContext()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
result = map.containsKey(key);
}
}
}
return result;
return result;
}

/**
* Gets the value from Hazelcast with the specified key in the given store.
* The store will be created if it does not already exist.
* @param storeName
* @param key
* @return
* @return
*/
public Serializable get(String storeName, Serializable key) {
Serializable result = null;
if (isEnabled()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
result = (Serializable) map.get(key);

if (result instanceof PayaraHazelcastEncryptedValueHolder && hzCore.isDatagridEncryptionEnabled()) {
result = (Serializable) HazelcastSymmetricEncryptor.byteArrayToObject(
HazelcastSymmetricEncryptor.decode(
((PayaraHazelcastEncryptedValueHolder) result).getEncryptedObjectBytes()));
try (Context ctx = ctxUtil.empty().pushContext()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
result = (Serializable) map.get(key);

if (result instanceof PayaraHazelcastEncryptedValueHolder && hzCore.isDatagridEncryptionEnabled()) {
result = (Serializable) HazelcastSymmetricEncryptor.byteArrayToObject(
HazelcastSymmetricEncryptor.decode(
((PayaraHazelcastEncryptedValueHolder) result).getEncryptedObjectBytes()));
}
}
}
}
Expand All @@ -220,19 +235,21 @@ public void event(Event event) {
public Map<Serializable, Serializable> getMap(String storeName) {
HashMap<Serializable,Serializable> result = new HashMap<>();
if (hzCore.isEnabled()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
Set<Serializable> keys = map.keySet();
for (Serializable key : keys) {
Serializable value = (Serializable) map.get(key);

if (value instanceof PayaraHazelcastEncryptedValueHolder && hzCore.isDatagridEncryptionEnabled()) {
value = (Serializable) HazelcastSymmetricEncryptor.byteArrayToObject(
HazelcastSymmetricEncryptor.decode(
((PayaraHazelcastEncryptedValueHolder) value).getEncryptedObjectBytes()));
}
try (Context ctx = ctxUtil.empty().pushContext()) {
IMap map = hzCore.getInstance().getMap(storeName);
if (map != null) {
Set<Serializable> keys = map.keySet();
for (Serializable key : keys) {
Serializable value = (Serializable) map.get(key);

result.put(key, value);
if (value instanceof PayaraHazelcastEncryptedValueHolder && hzCore.isDatagridEncryptionEnabled()) {
value = (Serializable) HazelcastSymmetricEncryptor.byteArrayToObject(
HazelcastSymmetricEncryptor.decode(
((PayaraHazelcastEncryptedValueHolder) value).getEncryptedObjectBytes()));
}

result.put(key, value);
}
}
}
}
Expand Down

0 comments on commit c2444a8

Please sign in to comment.