Skip to content

Commit

Permalink
map evict and clear revised
Browse files Browse the repository at this point in the history
  • Loading branch information
enesakar committed Jan 30, 2013
1 parent 9ba827c commit fcc78a5
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 85 deletions.
Expand Up @@ -74,7 +74,7 @@
Any integer between 0 and Integer.MAX_VALUE. 0 means
Integer.MAX_VALUE. Default is 0.
-->
<max-size policy="cluster_wide_map_size">5000</max-size>
<max-size policy="PER_JVM">5000</max-size>
<!--
When max. size is reached, specified percentage of
Expand All @@ -100,33 +100,28 @@
<para>
There are 5 defined policies can be used in max-size configuration.
<orderedlist>

<listitem>
<para>
<emphasis role="bold">cluster_wide_map_size:</emphasis> Cluster-wide total max map size (default policy).
<programlisting language="xml"><![CDATA[<max-size policy="cluster_wide_map_size">50000</max-size>]]></programlisting>
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">map_size_per_jvm:</emphasis> Max map size per JVM.
<emphasis role="bold">PER_JVM:</emphasis> Max map size per JVM.
<programlisting language="xml"><![CDATA[<max-size policy="map_size_per_jvm">5000</max-size>]]></programlisting>
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">partitions_wide_map_size:</emphasis> Partitions (default 271) wide max map size.
<emphasis role="bold">PER_PARTITION:</emphasis> Partitions (default 271) wide max map size.
<programlisting language="xml"><![CDATA[<max-size policy="partitions_wide_map_size">27100</max-size>]]></programlisting>
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">used_heap_size:</emphasis> Max used heap size in MB (mega-bytes) per JVM.
<emphasis role="bold">USED_HEAP_SIZE:</emphasis> Max used heap size in MB (mega-bytes) per JVM.
<programlisting language="xml"><![CDATA[<max-size policy="used_heap_size">4096</max-size>]]></programlisting>
</para>
</listitem>
<listitem>
<para>
<emphasis role="bold">used_heap_percentage:</emphasis> Max used heap size percentage per JVM.
<emphasis role="bold">USED_HEAP_PERCENTAGE:</emphasis> Max used heap size percentage per JVM.
<programlisting language="xml"><![CDATA[<max-size policy="used_heap_percentage">75</max-size>]]></programlisting>
</para>
</listitem>
Expand Down
16 changes: 10 additions & 6 deletions hazelcast/src/main/java/com/hazelcast/config/MapConfig.java
Expand Up @@ -40,7 +40,7 @@ public class MapConfig implements DataSerializable {
public final static int DEFAULT_TTL_SECONDS = 0;
public final static int DEFAULT_MAX_IDLE_SECONDS = 0;
public final static int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
public final static String DEFAULT_EVICTION_POLICY = "NONE";
public final static EvictionPolicy DEFAULT_EVICTION_POLICY = EvictionPolicy.NONE;
public final static RecordType DEFAULT_RECORD_TYPE = RecordType.DATA;
public final static String DEFAULT_MERGE_POLICY = AddNewEntryMergePolicy.NAME;

Expand All @@ -60,7 +60,7 @@ public class MapConfig implements DataSerializable {

private MaxSizeConfig maxSizeConfig = new MaxSizeConfig();

private String evictionPolicy = DEFAULT_EVICTION_POLICY;
private EvictionPolicy evictionPolicy = DEFAULT_EVICTION_POLICY;

private boolean valueIndexed = false;

Expand Down Expand Up @@ -90,6 +90,10 @@ public enum StorageType {
HEAP, OFFHEAP
}

public enum EvictionPolicy {
LRU, LFU, NONE
}

public MapConfig(String name) {
this.name = name;
}
Expand Down Expand Up @@ -380,14 +384,14 @@ public MapConfig setMaxSizeConfig(MaxSizeConfig maxSizeConfig) {
/**
* @return the evictionPolicy
*/
public String getEvictionPolicy() {
public EvictionPolicy getEvictionPolicy() {
return evictionPolicy;
}

/**
* @param evictionPolicy the evictionPolicy to set
*/
public MapConfig setEvictionPolicy(String evictionPolicy) {
public MapConfig setEvictionPolicy(EvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
return this;
}
Expand Down Expand Up @@ -585,7 +589,7 @@ public void readData(ObjectDataInput in) throws IOException {
boolean[] b = ByteUtil.fromByte(in.readByte());
valueIndexed = b[0];
readBackupData = b[1];
evictionPolicy = in.readUTF();
evictionPolicy = MapConfig.EvictionPolicy.valueOf(in.readUTF());
mergePolicy = in.readUTF();
// TODO: MapStoreConfig mapStoreConfig
// TODO: NearCacheConfig nearCacheConfig
Expand All @@ -602,7 +606,7 @@ public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(evictionDelaySeconds);
maxSizeConfig.writeData(out);
out.writeByte(ByteUtil.toByte(valueIndexed, readBackupData));
out.writeUTF(evictionPolicy);
out.writeUTF(evictionPolicy.name());
out.writeUTF(mergePolicy);
// TODO: MapStoreConfig mapStoreConfig
// TODO: NearCacheConfig nearCacheConfig
Expand Down
25 changes: 15 additions & 10 deletions hazelcast/src/main/java/com/hazelcast/config/MaxSizeConfig.java
Expand Up @@ -23,22 +23,26 @@
import java.io.IOException;

public class MaxSizeConfig implements DataSerializable {
public static final String POLICY_MAP_SIZE_PER_JVM = "map_size_per_jvm";
public static final String POLICY_CLUSTER_WIDE_MAP_SIZE = "cluster_wide_map_size";
public static final String POLICY_PARTITIONS_WIDE_MAP_SIZE = "partitions_wide_map_size";
public static final String POLICY_USED_HEAP_SIZE = "used_heap_size";
public static final String POLICY_USED_HEAP_PERCENTAGE = "used_heap_percentage";
int size = MapConfig.DEFAULT_MAX_SIZE;
String maxSizePolicy = POLICY_CLUSTER_WIDE_MAP_SIZE;
MaxSizePolicy maxSizePolicy = MaxSizePolicy.PER_JVM;

public enum MaxSizePolicy {
PER_JVM, PER_PARTITION, USED_HEAP_PERCENTAGE, USED_HEAP_SIZE
}

public static void main(String[] args) {
System.out.println(MaxSizePolicy.PER_JVM.name());

}

public void readData(ObjectDataInput in) throws IOException {
size = in.readInt();
maxSizePolicy = in.readUTF();
maxSizePolicy = MaxSizePolicy.valueOf(in.readUTF());
}

public void writeData(ObjectDataOutput out) throws IOException {
out.writeInt(size);
out.writeUTF(maxSizePolicy);
out.writeUTF(maxSizePolicy.name());
}

public int getSize() {
Expand All @@ -53,11 +57,12 @@ public MaxSizeConfig setSize(int size) {
return this;
}

public String getMaxSizePolicy() {
public MaxSizePolicy getMaxSizePolicy() {
return maxSizePolicy;
}

public MaxSizeConfig setMaxSizePolicy(String maxSizePolicy) {
// todo make policy enum
public MaxSizeConfig setMaxSizePolicy(MaxSizePolicy maxSizePolicy) {
this.maxSizePolicy = maxSizePolicy;
return this;
}
Expand Down
Expand Up @@ -685,12 +685,12 @@ private void handleMap(final org.w3c.dom.Node node) throws Exception {
} else if ("async-backup-count".equals(nodeName)) {
mapConfig.setAsyncBackupCount(getIntegerValue("async-backup-count", value, MapConfig.MIN_BACKUP_COUNT));
} else if ("eviction-policy".equals(nodeName)) {
mapConfig.setEvictionPolicy(value);
mapConfig.setEvictionPolicy(MapConfig.EvictionPolicy.valueOf(value));
} else if ("max-size".equals(nodeName)) {
final MaxSizeConfig msc = mapConfig.getMaxSizeConfig();
final Node maxSizePolicy = n.getAttributes().getNamedItem("policy");
if (maxSizePolicy != null) {
msc.setMaxSizePolicy(getTextContent(maxSizePolicy));
msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.valueOf(getTextContent(maxSizePolicy)));
}
int size = 0;
if (value.length() < 2) {
Expand Down
104 changes: 104 additions & 0 deletions hazelcast/src/main/java/com/hazelcast/map/ClearBackupOperation.java
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.hazelcast.map;

import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.DataSerializable;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.BackupAwareOperation;
import com.hazelcast.spi.BackupOperation;
import com.hazelcast.spi.Operation;
import com.hazelcast.spi.PartitionLevelOperation;
import com.hazelcast.spi.impl.AbstractNamedOperation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ClearBackupOperation extends AbstractNamedOperation implements BackupOperation, DataSerializable {

Set<Data> keys;
MapService mapService;
RecordStore recordStore;

public ClearBackupOperation(String name, Set<Data> keys) {
super(name);
this.keys = keys;
}

public ClearBackupOperation() {
}

@Override
public void beforeRun() throws Exception {
mapService = getService();
recordStore = mapService.getRecordStore(getPartitionId(), name);
}

public void run() {
if (keys == null) {
recordStore.removeAll();
return;
}
for (Data key : keys) {
if (!recordStore.getLocks().containsKey(key))
recordStore.remove(key);
}
}


@Override
protected void writeInternal(ObjectDataOutput out) throws IOException {
super.writeInternal(out);
out.writeUTF(name);
if (keys == null)
out.writeInt(-1);
else {
out.writeInt(keys.size());
for (Data key : keys) {
key.writeData(out);
}
}
}

@Override
protected void readInternal(ObjectDataInput in) throws IOException {
super.readInternal(in);
name = in.readUTF();
int size = in.readInt();
if(size > -1) {
keys = new HashSet<Data>(size);
for (int i = 0; i < size; i++) {
Data data = new Data();
data.readData(in);
keys.add(data);
}
}
}


@Override
public String toString() {
return "ClearBackupOperation{" +
'}';
}

}

0 comments on commit fcc78a5

Please sign in to comment.