Skip to content

Commit

Permalink
ARTEMIS-3645 Support broker balancer cache persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
brusdev authored and jbertram committed Feb 8, 2022
1 parent ae7e7cb commit 290e501
Show file tree
Hide file tree
Showing 29 changed files with 755 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BrokerBalancerConfiguration implements Serializable {
private TargetKey targetKey = TargetKey.SOURCE_IP;
private String targetKeyFilter = null;
private String localTargetFilter = null;
private int cacheTimeout = -1;
private CacheConfiguration cacheConfiguration = null;
private PoolConfiguration poolConfiguration = null;
private NamedPropertyConfiguration policyConfiguration = null;
private NamedPropertyConfiguration transformerConfiguration = null;
Expand Down Expand Up @@ -67,12 +67,12 @@ public BrokerBalancerConfiguration setLocalTargetFilter(String localTargetFilter
return this;
}

public int getCacheTimeout() {
return cacheTimeout;
public CacheConfiguration getCacheConfiguration() {
return cacheConfiguration;
}

public BrokerBalancerConfiguration setCacheTimeout(int cacheTimeout) {
this.cacheTimeout = cacheTimeout;
public BrokerBalancerConfiguration setCacheConfiguration(CacheConfiguration cacheConfiguration) {
this.cacheConfiguration = cacheConfiguration;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.activemq.artemis.core.config.balancing;

import java.io.Serializable;

public class CacheConfiguration implements Serializable {
private boolean persisted = false;

private int timeout = 0;

public CacheConfiguration() {
}

public boolean isPersisted() {
return persisted;
}

public CacheConfiguration setPersisted(boolean persisted) {
this.persisted = persisted;
return this;
}

public int getTimeout() {
return timeout;
}

public CacheConfiguration setTimeout(int timeout) {
this.timeout = timeout;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.activemq.artemis.api.core.UDPBroadcastEndpointFactory;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.core.config.balancing.BrokerBalancerConfiguration;
import org.apache.activemq.artemis.core.config.balancing.CacheConfiguration;
import org.apache.activemq.artemis.core.config.balancing.NamedPropertyConfiguration;
import org.apache.activemq.artemis.core.config.amqpBrokerConnectivity.AMQPBrokerConnectConfiguration;
import org.apache.activemq.artemis.core.config.BridgeConfiguration;
Expand Down Expand Up @@ -2653,17 +2654,18 @@ private void parseBalancerConfiguration(final Element e, final Configuration con

brokerBalancerConfiguration.setLocalTargetFilter(getString(e, "local-target-filter", brokerBalancerConfiguration.getLocalTargetFilter(), Validators.NO_CHECK));

brokerBalancerConfiguration.setCacheTimeout(getInteger(e, "cache-timeout",
brokerBalancerConfiguration.getCacheTimeout(), Validators.MINUS_ONE_OR_GE_ZERO));

NamedPropertyConfiguration policyConfiguration = null;
PoolConfiguration poolConfiguration = null;
NodeList children = e.getChildNodes();

for (int j = 0; j < children.getLength(); j++) {
Node child = children.item(j);

if (child.getNodeName().equals("policy")) {
if (child.getNodeName().equals("cache")) {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
parseCacheConfiguration((Element) child, cacheConfiguration);
brokerBalancerConfiguration.setCacheConfiguration(cacheConfiguration);
} else if (child.getNodeName().equals("policy")) {
policyConfiguration = new NamedPropertyConfiguration();
parsePolicyConfiguration((Element) child, policyConfiguration);
brokerBalancerConfiguration.setPolicyConfiguration(policyConfiguration);
Expand All @@ -2681,6 +2683,14 @@ private void parseBalancerConfiguration(final Element e, final Configuration con
config.getBalancerConfigurations().add(brokerBalancerConfiguration);
}

private void parseCacheConfiguration(final Element e, final CacheConfiguration cacheConfiguration) throws ClassNotFoundException {
cacheConfiguration.setPersisted(getBoolean(e, "persisted",
cacheConfiguration.isPersisted()));

cacheConfiguration.setTimeout(getInteger(e, "timeout",
cacheConfiguration.getTimeout(), Validators.GE_ZERO));
}

private void parseTransformerConfiguration(final Element e, final NamedPropertyConfiguration policyConfiguration) throws ClassNotFoundException {
String name = e.getAttribute("name");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.activemq.artemis.core.paging.cursor.PagePosition;
import org.apache.activemq.artemis.core.persistence.config.PersistedAddressSetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedDivertConfiguration;
import org.apache.activemq.artemis.core.persistence.config.PersistedKeyValuePair;
import org.apache.activemq.artemis.core.persistence.config.PersistedRole;
import org.apache.activemq.artemis.core.persistence.config.PersistedSecuritySetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedUser;
Expand Down Expand Up @@ -383,6 +384,12 @@ JournalLoadInformation loadBindingJournal(List<QueueBindingInfo> queueBindingInf

Map<String, PersistedRole> getPersistedRoles();

void storeKeyValuePair(PersistedKeyValuePair persistedKeyValuePair) throws Exception;

void deleteKeyValuePair(String mapId, String key) throws Exception;

Map<String, PersistedKeyValuePair> getPersistedKeyValuePairs(String mapId);

/**
* @return The ID with the stored counter
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.activemq.artemis.core.persistence.config;

import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.core.journal.EncodingSupport;
import org.apache.activemq.artemis.utils.BufferHelper;

public class PersistedKeyValuePair implements EncodingSupport {

private long storeId;

private String mapId;

private String key;

private String value;

public PersistedKeyValuePair() {
}

public PersistedKeyValuePair(String mapId, String key, String value) {
this.mapId = mapId;
this.key = key;
this.value = value;
}

public void setStoreId(long id) {
this.storeId = id;
}

public long getStoreId() {
return storeId;
}

public String getMapId() {
return mapId;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

@Override
public int getEncodeSize() {
int size = 0;
size += BufferHelper.sizeOfString(mapId);
size += BufferHelper.sizeOfString(key);
size += BufferHelper.sizeOfString(value);
return size;
}

@Override
public void encode(ActiveMQBuffer buffer) {
buffer.writeString(mapId);
buffer.writeString(key);
buffer.writeString(value);
}

@Override
public void decode(ActiveMQBuffer buffer) {
mapId = buffer.readString();
key = buffer.readString();
value = buffer.readString();
}

@Override
public String toString() {
return "PersistedKeyValuePair [storeId=" + storeId +
", mapId=" +
mapId +
", key=" +
key +
", value=" +
value +
"]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.apache.activemq.artemis.core.persistence.StorageManager;
import org.apache.activemq.artemis.core.persistence.config.PersistedAddressSetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedDivertConfiguration;
import org.apache.activemq.artemis.core.persistence.config.PersistedKeyValuePair;
import org.apache.activemq.artemis.core.persistence.config.PersistedRole;
import org.apache.activemq.artemis.core.persistence.config.PersistedSecuritySetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedUser;
Expand Down Expand Up @@ -220,6 +221,8 @@ public Configuration getConfig() {

protected final Map<String, PersistedRole> mapPersistedRoles = new ConcurrentHashMap<>();

protected final Map<String, Map<String, PersistedKeyValuePair>> mapPersistedKeyValuePairs = new ConcurrentHashMap<>();

protected final ConcurrentLongHashMap<LargeServerMessage> largeMessagesToDelete = new ConcurrentLongHashMap<>();

public AbstractJournalStorageManager(final Configuration config,
Expand Down Expand Up @@ -818,6 +821,41 @@ public Map<String, PersistedRole> getPersistedRoles() {
return new HashMap<>(mapPersistedRoles);
}

@Override
public void storeKeyValuePair(PersistedKeyValuePair persistedKeyValuePair) throws Exception {
deleteKeyValuePair(persistedKeyValuePair.getMapId(), persistedKeyValuePair.getKey());
try (ArtemisCloseable lock = closeableReadLock()) {
final long id = idGenerator.generateID();
persistedKeyValuePair.setStoreId(id);
bindingsJournal.appendAddRecord(id, JournalRecordIds.KEY_VALUE_PAIR_RECORD, persistedKeyValuePair, true);
Map<String, PersistedKeyValuePair> persistedKeyValuePairs = mapPersistedKeyValuePairs.get(persistedKeyValuePair.getMapId());
if (persistedKeyValuePairs == null) {
persistedKeyValuePairs = new HashMap<>();
mapPersistedKeyValuePairs.put(persistedKeyValuePair.getMapId(), persistedKeyValuePairs);
}
persistedKeyValuePairs.put(persistedKeyValuePair.getKey(), persistedKeyValuePair);
}
}

@Override
public void deleteKeyValuePair(String mapId, String key) throws Exception {
Map<String, PersistedKeyValuePair> persistedKeyValuePairs = mapPersistedKeyValuePairs.get(mapId);
if (persistedKeyValuePairs != null) {
PersistedKeyValuePair oldMapStringEntry = persistedKeyValuePairs.remove(key);
if (oldMapStringEntry != null) {
try (ArtemisCloseable lock = closeableReadLock()) {
bindingsJournal.tryAppendDeleteRecord(oldMapStringEntry.getStoreId(), this::recordNotFoundCallback, false);
}
}
}
}

@Override
public Map<String, PersistedKeyValuePair> getPersistedKeyValuePairs(String mapId) {
Map<String, PersistedKeyValuePair> persistedKeyValuePairs = mapPersistedKeyValuePairs.get(mapId);
return persistedKeyValuePairs != null ? new HashMap<>(persistedKeyValuePairs) : new HashMap<>();
}

@Override
public void storeID(final long journalID, final long id) throws Exception {
try (ArtemisCloseable lock = closeableReadLock()) {
Expand Down Expand Up @@ -1534,6 +1572,14 @@ public JournalLoadInformation loadBindingJournal(final List<QueueBindingInfo> qu
} else if (rec == JournalRecordIds.ROLE_RECORD) {
PersistedRole role = newRoleEncoding(id, buffer);
mapPersistedRoles.put(role.getUsername(), role);
} else if (rec == JournalRecordIds.KEY_VALUE_PAIR_RECORD) {
PersistedKeyValuePair keyValuePair = newKeyValuePairEncoding(id, buffer);
Map<String, PersistedKeyValuePair> persistedKeyValuePairs = mapPersistedKeyValuePairs.get(keyValuePair.getMapId());
if (persistedKeyValuePairs == null) {
persistedKeyValuePairs = new HashMap<>();
mapPersistedKeyValuePairs.put(keyValuePair.getMapId(), persistedKeyValuePairs);
}
persistedKeyValuePairs.put(keyValuePair.getKey(), keyValuePair);
} else {
// unlikely to happen
ActiveMQServerLogger.LOGGER.invalidRecordType(rec, new Exception("invalid record type " + rec));
Expand Down Expand Up @@ -2046,6 +2092,13 @@ static PersistedRole newRoleEncoding(long id, ActiveMQBuffer buffer) {
return persistedRole;
}

static PersistedKeyValuePair newKeyValuePairEncoding(long id, ActiveMQBuffer buffer) {
PersistedKeyValuePair persistedKeyValuePair = new PersistedKeyValuePair();
persistedKeyValuePair.decode(buffer);
persistedKeyValuePair.setStoreId(id);
return persistedKeyValuePair;
}

/**
* @param id
* @param buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ public final class JournalRecordIds {
// Used to record the large message body on the journal when history is on
public static final byte ADD_MESSAGE_BODY = 49;

public static final byte KEY_VALUE_PAIR_RECORD = 50;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.activemq.artemis.core.persistence.StorageManager;
import org.apache.activemq.artemis.core.persistence.config.PersistedAddressSetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedDivertConfiguration;
import org.apache.activemq.artemis.core.persistence.config.PersistedKeyValuePair;
import org.apache.activemq.artemis.core.persistence.config.PersistedRole;
import org.apache.activemq.artemis.core.persistence.config.PersistedSecuritySetting;
import org.apache.activemq.artemis.core.persistence.config.PersistedUser;
Expand Down Expand Up @@ -488,6 +489,21 @@ public Map<String, PersistedRole> getPersistedRoles() {
return null;
}

@Override
public void storeKeyValuePair(PersistedKeyValuePair persistedKeyValuePair) throws Exception {

}

@Override
public void deleteKeyValuePair(String mapId, String key) throws Exception {

}

@Override
public Map<String, PersistedKeyValuePair> getPersistedKeyValuePairs(String mapId) {
return null;
}

@Override
public void storeSecuritySetting(final PersistedSecuritySetting persistedRoles) throws Exception {
}
Expand Down

0 comments on commit 290e501

Please sign in to comment.