Skip to content

Commit

Permalink
WFLY-7078 Further optimize size of fine session attribute names cache…
Browse files Browse the repository at this point in the history
… entry
  • Loading branch information
pferraro committed Sep 8, 2016
1 parent 6f9f520 commit 9b82616
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 17 deletions.
Expand Up @@ -123,7 +123,7 @@ public void writeData(DataOutput output, int index) throws IOException {
public abstract void writeData(DataOutput output, int index) throws IOException;

@Override
public Integer readObject(ObjectInput input) throws IOException, ClassNotFoundException {
public Integer readObject(ObjectInput input) throws IOException {
return Integer.valueOf(this.readData(input));
}

Expand Down
Expand Up @@ -50,32 +50,32 @@
* A separate cache entry stores the activate attribute names for the session.
* @author Paul Ferraro
*/
public class FineSessionAttributesFactory implements SessionAttributesFactory<Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>>> {
public class FineSessionAttributesFactory implements SessionAttributesFactory<SessionAttributeNamesEntry> {

private final Cache<SessionAttributeNamesKey, Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>>> namesCache;
private final Cache<SessionAttributeNamesKey, SessionAttributeNamesEntry> namesCache;
private final Cache<SessionAttributeKey, MarshalledValue<Object, MarshallingContext>> attributeCache;
private final Marshaller<Object, MarshalledValue<Object, MarshallingContext>, MarshallingContext> marshaller;
private final CacheProperties properties;

public FineSessionAttributesFactory(Cache<SessionAttributeNamesKey, Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>>> namesCache, Cache<SessionAttributeKey, MarshalledValue<Object, MarshallingContext>> attributeCache, Marshaller<Object, MarshalledValue<Object, MarshallingContext>, MarshallingContext> marshaller, CacheProperties properties) {
public FineSessionAttributesFactory(Cache<SessionAttributeNamesKey, SessionAttributeNamesEntry> namesCache, Cache<SessionAttributeKey, MarshalledValue<Object, MarshallingContext>> attributeCache, Marshaller<Object, MarshalledValue<Object, MarshallingContext>, MarshallingContext> marshaller, CacheProperties properties) {
this.namesCache = namesCache;
this.attributeCache = attributeCache;
this.marshaller = marshaller;
this.properties = properties;
}

@Override
public Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> createValue(String id, Void context) {
Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry = new AbstractMap.SimpleImmutableEntry<>(new AtomicInteger(), new ConcurrentHashMap<>());
public SessionAttributeNamesEntry createValue(String id, Void context) {
SessionAttributeNamesEntry entry = new SessionAttributeNamesEntry(new AtomicInteger(), new ConcurrentHashMap<>());
this.namesCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(new SessionAttributeNamesKey(id), entry);
return entry;
}

@Override
public Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> findValue(String id) {
Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry = this.namesCache.get(new SessionAttributeNamesKey(id));
public SessionAttributeNamesEntry findValue(String id) {
SessionAttributeNamesEntry entry = this.namesCache.get(new SessionAttributeNamesKey(id));
if (entry != null) {
ConcurrentMap<String, Integer> names = entry.getValue();
ConcurrentMap<String, Integer> names = entry.getNames();
Map<SessionAttributeKey, MarshalledValue<Object, MarshallingContext>> attributes = this.attributeCache.getAdvancedCache().getAll(names.values().stream().map(attributeId -> new SessionAttributeKey(id, attributeId)).collect(Collectors.toSet()));
Predicate<Map.Entry<String, MarshalledValue<Object, MarshallingContext>>> invalidAttribute = attribute -> {
try {
Expand All @@ -97,18 +97,18 @@ public Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> findValue(String

@Override
public boolean remove(String id) {
Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry = this.namesCache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS).remove(new SessionAttributeNamesKey(id));
SessionAttributeNamesEntry entry = this.namesCache.getAdvancedCache().withFlags(Flag.FORCE_SYNCHRONOUS).remove(new SessionAttributeNamesKey(id));
if (entry == null) return false;
entry.getValue().values().forEach(attributeId -> this.attributeCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(new SessionAttributeKey(id, attributeId)));
entry.getNames().values().forEach(attributeId -> this.attributeCache.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).remove(new SessionAttributeKey(id, attributeId)));
return true;
}

@Override
public void evict(String id) {
SessionAttributeNamesKey key = new SessionAttributeNamesKey(id);
Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry = this.namesCache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).get(key);
SessionAttributeNamesEntry entry = this.namesCache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_LOAD).get(key);
if (entry != null) {
entry.getValue().entrySet().stream().forEach(attribute -> {
entry.getNames().entrySet().stream().forEach(attribute -> {
try {
this.attributeCache.evict(new SessionAttributeKey(id, attribute.getValue()));
} catch (Throwable e) {
Expand All @@ -120,14 +120,14 @@ public void evict(String id) {
}

@Override
public SessionAttributes createSessionAttributes(String id, Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry) {
public SessionAttributes createSessionAttributes(String id, SessionAttributeNamesEntry entry) {
SessionAttributeNamesKey key = new SessionAttributeNamesKey(id);
Mutator mutator = this.properties.isTransactional() && this.namesCache.getAdvancedCache().getCacheEntry(key).isCreated() ? Mutator.PASSIVE : new CacheEntryMutator<>(this.namesCache, key, entry);
return new FineSessionAttributes<>(id, entry.getKey(), entry.getValue(), mutator, this.attributeCache, this.marshaller, this.properties);
return new FineSessionAttributes<>(id, entry.getSequence(), entry.getNames(), mutator, this.attributeCache, this.marshaller, this.properties);
}

@Override
public ImmutableSessionAttributes createImmutableSessionAttributes(String id, Map.Entry<AtomicInteger, ConcurrentMap<String, Integer>> entry) {
return new FineImmutableSessionAttributes<>(id, entry.getValue(), this.attributeCache, this.marshaller);
public ImmutableSessionAttributes createImmutableSessionAttributes(String id, SessionAttributeNamesEntry entry) {
return new FineImmutableSessionAttributes<>(id, entry.getNames(), this.attributeCache, this.marshaller);
}
}
@@ -0,0 +1,48 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.clustering.web.infinispan.session.fine;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Cache entry containing the names of the attributes of a session.
* @author Paul Ferraro
*/
public class SessionAttributeNamesEntry {
private final AtomicInteger sequence;
private final ConcurrentMap<String, Integer> names;

public SessionAttributeNamesEntry(AtomicInteger sequence, ConcurrentMap<String, Integer> names) {
this.sequence = sequence;
this.names = names;
}

public AtomicInteger getSequence() {
return this.sequence;
}

public ConcurrentMap<String, Integer> getNames() {
return this.names;
}
}
@@ -0,0 +1,68 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.clustering.web.infinispan.session.fine;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.jboss.IndexExternalizer;

/**
* Externalizer for {@link SessionAttributeNamesEntry}.
* @author Paul Ferraro
*/
public class SessionAttributeNamesEntryExternalizer implements Externalizer<SessionAttributeNamesEntry> {

@Override
public void writeObject(ObjectOutput output, SessionAttributeNamesEntry value) throws IOException {
IndexExternalizer.VARIABLE.writeData(output, value.getSequence().get());
ConcurrentMap<String, Integer> names = value.getNames();
IndexExternalizer.VARIABLE.writeData(output, names.size());
for (Map.Entry<String, Integer> entry : names.entrySet()) {
output.writeUTF(entry.getKey());
IndexExternalizer.VARIABLE.writeObject(output, entry.getValue());
}
}

@Override
public SessionAttributeNamesEntry readObject(ObjectInput input) throws IOException {
AtomicInteger sequence = new AtomicInteger(IndexExternalizer.VARIABLE.readData(input));
int size = IndexExternalizer.VARIABLE.readData(input);
ConcurrentMap<String, Integer> names = new ConcurrentHashMap<>(size);
for (int i = 0; i < size; ++i) {
names.put(input.readUTF(), IndexExternalizer.VARIABLE.readObject(input));
}
return new SessionAttributeNamesEntry(sequence, names);
}

@Override
public Class<? extends SessionAttributeNamesEntry> getTargetClass() {
return SessionAttributeNamesEntry.class;
}
}
@@ -1,6 +1,7 @@
org.wildfly.clustering.web.infinispan.session.coarse.SessionAttributesKeyExternalizer
org.wildfly.clustering.web.infinispan.session.fine.SessionAttributeKeyExternalizer
org.wildfly.clustering.web.infinispan.session.fine.SessionAttributeNamesKeyExternalizer
org.wildfly.clustering.web.infinispan.session.fine.SessionAttributeNamesEntryExternalizer
org.wildfly.clustering.web.infinispan.session.SessionAccessMetaDataExternalizer
org.wildfly.clustering.web.infinispan.session.SessionAccessMetaDataKeyExternalizer
org.wildfly.clustering.web.infinispan.session.SessionCreationMetaDataEntryExternalizer
Expand Down

0 comments on commit 9b82616

Please sign in to comment.