ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agoncha...@apache.org
Subject ignite git commit: IGNITE-5025 - Do not allow null names for memory policies
Date Tue, 25 Apr 2017 08:22:37 GMT
Repository: ignite
Updated Branches:
  refs/heads/ignite-2.0 4739458cd -> 1dc9e6985


IGNITE-5025 - Do not allow null names for memory policies


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/1dc9e698
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/1dc9e698
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/1dc9e698

Branch: refs/heads/ignite-2.0
Commit: 1dc9e69851c19610e9357e35ae17ef2599733e5f
Parents: 4739458
Author: Sergey Chugunov <sergey.chugunov@gmail.com>
Authored: Tue Apr 25 11:17:24 2017 +0300
Committer: Alexey Goncharuk <alexey.goncharuk@gmail.com>
Committed: Tue Apr 25 11:23:30 2017 +0300

----------------------------------------------------------------------
 .../configuration/MemoryConfiguration.java      |  12 +-
 .../MemoryPolicyConfiguration.java              |   6 +-
 .../IgniteCacheDatabaseSharedManager.java       | 130 ++++----
 .../cache/MemoryPolicyConfigValidationTest.java |   6 +-
 .../MemoryPolicyInitializationTest.java         | 307 +++++++++++++++++++
 .../testsuites/IgniteCacheTestSuite2.java       |   2 +
 .../Cache/Configuration/MemoryConfiguration.cs  |   7 +
 .../Configuration/MemoryPolicyConfiguration.cs  |   3 +
 8 files changed, 415 insertions(+), 58 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/main/java/org/apache/ignite/configuration/MemoryConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/configuration/MemoryConfiguration.java
b/modules/core/src/main/java/org/apache/ignite/configuration/MemoryConfiguration.java
index cd7c1fe..f88a95a 100644
--- a/modules/core/src/main/java/org/apache/ignite/configuration/MemoryConfiguration.java
+++ b/modules/core/src/main/java/org/apache/ignite/configuration/MemoryConfiguration.java
@@ -71,6 +71,9 @@ public class MemoryConfiguration implements Serializable {
     /** Default memory page size. */
     public static final int DFLT_PAGE_SIZE = 2 * 1024;
 
+    /** This name is assigned to default MemoryPolicy if no user-defined default MemPlc is
specified */
+    public static final String DFLT_MEM_PLC_DEFAULT_NAME = "default";
+
     /** Size of a memory chunk reserved for system cache needs. */
     private long sysCacheMemSize = DFLT_SYS_CACHE_MEM_SIZE;
 
@@ -81,7 +84,7 @@ public class MemoryConfiguration implements Serializable {
     private int concLvl;
 
     /** A name of the memory policy that defines the default memory region. */
-    private String dfltMemPlcName;
+    private String dfltMemPlcName = DFLT_MEM_PLC_DEFAULT_NAME;
 
     /** Memory policies. */
     private MemoryPolicyConfiguration[] memPlcs;
@@ -98,6 +101,8 @@ public class MemoryConfiguration implements Serializable {
     /**
      * Sets the size of a memory chunk reserved for system cache needs.
      *
+     * Default value is {@link #DFLT_SYS_CACHE_MEM_SIZE}
+     *
      * @param sysCacheMemSize Size in bytes.
      */
     public MemoryConfiguration setSystemCacheMemorySize(long sysCacheMemSize) {
@@ -119,6 +124,8 @@ public class MemoryConfiguration implements Serializable {
     /**
      * Changes the page size.
      *
+     * Default value is {@link #DFLT_PAGE_SIZE}
+     *
      * @param pageSize Page size in bytes.
      */
     public MemoryConfiguration setPageSize(int pageSize) {
@@ -164,7 +171,6 @@ public class MemoryConfiguration implements Serializable {
     public MemoryPolicyConfiguration createDefaultPolicyConfig() {
         MemoryPolicyConfiguration memPlc = new MemoryPolicyConfiguration();
 
-        memPlc.setName(null);
         memPlc.setSize(DFLT_MEMORY_POLICY_SIZE);
 
         return memPlc;
@@ -207,6 +213,8 @@ public class MemoryConfiguration implements Serializable {
      * {@link MemoryConfiguration#setMemoryPolicies(MemoryPolicyConfiguration...)} method
and change the name of the
      * default memory policy with {@code MemoryConfiguration#setDefaultMemoryPolicyName(String)}.
      *
+     * If nothing is specified by user, it is set to {@link #DFLT_MEM_PLC_DEFAULT_NAME} value.
+     *
      * @param dfltMemPlcName Name of a memory policy to be used as default one.
      */
     public MemoryConfiguration setDefaultMemoryPolicyName(String dfltMemPlcName) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/main/java/org/apache/ignite/configuration/MemoryPolicyConfiguration.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/configuration/MemoryPolicyConfiguration.java
b/modules/core/src/main/java/org/apache/ignite/configuration/MemoryPolicyConfiguration.java
index 6d5fe8c..d0adcf9 100644
--- a/modules/core/src/main/java/org/apache/ignite/configuration/MemoryPolicyConfiguration.java
+++ b/modules/core/src/main/java/org/apache/ignite/configuration/MemoryPolicyConfiguration.java
@@ -19,6 +19,8 @@ package org.apache.ignite.configuration;
 import java.io.Serializable;
 import org.apache.ignite.internal.mem.IgniteOutOfMemoryException;
 
+import static org.apache.ignite.configuration.MemoryConfiguration.DFLT_MEM_PLC_DEFAULT_NAME;
+
 /**
  * This class allows defining custom memory policies' configurations with various parameters
for Apache Ignite
  * page memory (see {@link MemoryConfiguration}. For each configured memory policy Apache
Ignite instantiates
@@ -61,7 +63,7 @@ public final class MemoryPolicyConfiguration implements Serializable {
     private static final long serialVersionUID = 0L;
 
     /** Memory policy name. */
-    private String name;
+    private String name = DFLT_MEM_PLC_DEFAULT_NAME;
 
     /** Memory policy maximum size. */
     private long size;
@@ -93,6 +95,8 @@ public final class MemoryPolicyConfiguration implements Serializable {
     /**
      * Sets memory policy name. The name must be non empty and must not be equal to the reserved
'sysMemPlc' one.
      *
+     * If not specified, {@link MemoryConfiguration#DFLT_MEM_PLC_DEFAULT_NAME} value is used.
+     *
      * @param name Memory policy name.
      */
     public MemoryPolicyConfiguration setName(String name) {

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/IgniteCacheDatabaseSharedManager.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/IgniteCacheDatabaseSharedManager.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/IgniteCacheDatabaseSharedManager.java
index 705e74c..ae594fa 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/IgniteCacheDatabaseSharedManager.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/IgniteCacheDatabaseSharedManager.java
@@ -57,12 +57,14 @@ import org.apache.ignite.internal.processors.cluster.IgniteChangeGlobalStateSupp
 import org.apache.ignite.mxbean.MemoryMetricsMXBean;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.ignite.configuration.MemoryConfiguration.DFLT_MEM_PLC_DEFAULT_NAME;
+
 /**
  *
  */
 public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdapter implements
IgniteChangeGlobalStateSupport {
     /** MemoryPolicyConfiguration name reserved for internal caches. */
-    private static final String SYSTEM_MEMORY_POLICY_NAME = "sysMemPlc";
+    static final String SYSTEM_MEMORY_POLICY_NAME = "sysMemPlc";
 
     /** Minimum size of memory chunk */
     private static final long MIN_PAGE_MEMORY_SIZE = 1024 * 1024;
@@ -109,6 +111,8 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap
 
             initPageMemoryPolicies(memCfg);
 
+            registerMetricsMBeans();
+
             startMemoryPolicies();
 
             initPageMemoryDataStructures(memCfg);
@@ -116,6 +120,34 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap
     }
 
     /**
+     * Registers MBeans for all MemoryMetrics configured in this instance.
+     */
+    private void registerMetricsMBeans() {
+        IgniteConfiguration cfg = cctx.gridConfig();
+
+        for (MemoryMetrics memMetrics : memMetricsMap.values())
+            registerMetricsMBean((MemoryMetricsImpl) memMetrics, cfg);
+    }
+
+    /**
+     * @param memMetrics Memory metrics.
+     */
+    private void registerMetricsMBean(MemoryMetricsImpl memMetrics, IgniteConfiguration cfg)
{
+        try {
+            U.registerMBean(
+                    cfg.getMBeanServer(),
+                    cfg.getIgniteInstanceName(),
+                    "MemoryMetrics",
+                    memMetrics.getName(),
+                    memMetrics,
+                    MemoryMetricsMXBean.class);
+        }
+        catch (JMException e) {
+            log.warning("Failed to register MBean for MemoryMetrics with name: '" + memMetrics.getName()
+ "'");
+        }
+    }
+
+    /**
      * @param dbCfg Database config.
      */
     protected void initPageMemoryDataStructures(MemoryConfiguration dbCfg) throws IgniteCheckedException
{
@@ -172,87 +204,81 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap
         if (memPlcsCfgs == null) {
             //reserve place for default and system memory policies
             memPlcMap = U.newHashMap(2);
-
             memMetricsMap = U.newHashMap(2);
 
-            MemoryPolicyConfiguration dfltPlcCfg = dbCfg.createDefaultPolicyConfig();
-
-            MemoryMetricsImpl memMetrics = new MemoryMetricsImpl(dfltPlcCfg);
-
-            registerMetricsMBean(memMetrics);
-
-            dfltMemPlc = createDefaultMemoryPolicy(dbCfg, dfltPlcCfg, memMetrics);
-
-            memPlcMap.put(null, dfltMemPlc);
-            memMetricsMap.put(null, memMetrics);
+            addMemoryPolicy(dbCfg,
+                    dbCfg.createDefaultPolicyConfig(),
+                    DFLT_MEM_PLC_DEFAULT_NAME);
 
             log.warning("No user-defined default MemoryPolicy found; system default of 1GB
size will be used.");
         }
         else {
             String dfltMemPlcName = dbCfg.getDefaultMemoryPolicyName();
 
-            if (dfltMemPlcName == null) {
+            if (DFLT_MEM_PLC_DEFAULT_NAME.equals(dfltMemPlcName) && !hasCustomDefaultMemoryPolicy(memPlcsCfgs))
{
                 //reserve additional place for default and system memory policies
                 memPlcMap = U.newHashMap(memPlcsCfgs.length + 2);
                 memMetricsMap = U.newHashMap(memPlcsCfgs.length + 2);
 
-                MemoryPolicyConfiguration dfltPlcCfg = dbCfg.createDefaultPolicyConfig();
-
-                MemoryMetricsImpl memMetrics = new MemoryMetricsImpl(dfltPlcCfg);
+                addMemoryPolicy(dbCfg,
+                        dbCfg.createDefaultPolicyConfig(),
+                        DFLT_MEM_PLC_DEFAULT_NAME);
 
-                dfltMemPlc = createDefaultMemoryPolicy(dbCfg, dfltPlcCfg, memMetrics);
-                memPlcMap.put(null, dfltMemPlc);
-                memMetricsMap.put(null, memMetrics);
-
-                log.warning("No user-defined default MemoryPolicy found; system default of
1GB size will be used.");
+                log.warning("No user-defined default MemoryPolicy found; system default will
be allocated.");
             }
             else {
-                //reserve additional place for system memory policy only
+                //reserve additional space for system memory policy only
                 memPlcMap = U.newHashMap(memPlcsCfgs.length + 1);
-                memMetricsMap = U.newHashMap(memPlcsCfgs.length + 1);;
+                memMetricsMap = U.newHashMap(memPlcsCfgs.length + 1);
             }
 
-            for (MemoryPolicyConfiguration memPlcCfg : memPlcsCfgs) {
-                MemoryMetricsImpl memMetrics = new MemoryMetricsImpl(memPlcCfg);
+            for (MemoryPolicyConfiguration memPlcCfg : memPlcsCfgs)
+                addMemoryPolicy(dbCfg, memPlcCfg, memPlcCfg.getName());
+        }
 
-                MemoryPolicy memPlc = initMemory(dbCfg, memPlcCfg, memMetrics);
+        addMemoryPolicy(dbCfg,
+                createSystemMemoryPolicy(dbCfg.getSystemCacheMemorySize()),
+                SYSTEM_MEMORY_POLICY_NAME);
+    }
 
-                memPlcMap.put(memPlcCfg.getName(), memPlc);
+    /**
+     * @param dbCfg Database config.
+     * @param memPlcCfg Memory policy config.
+     * @param memPlcName Memory policy name.
+     */
+    private void addMemoryPolicy(MemoryConfiguration dbCfg,
+                                 MemoryPolicyConfiguration memPlcCfg,
+                                 String memPlcName) {
+        String dfltMemPlcName = dbCfg.getDefaultMemoryPolicyName();
 
-                memMetricsMap.put(memPlcCfg.getName(), memMetrics);
+        if (dfltMemPlcName == null)
+            dfltMemPlcName = DFLT_MEM_PLC_DEFAULT_NAME;
 
-                if (memPlcCfg.getName().equals(dfltMemPlcName))
-                    dfltMemPlc = memPlc;
-            }
-        }
+        MemoryMetricsImpl memMetrics = new MemoryMetricsImpl(memPlcCfg);
 
-        MemoryPolicyConfiguration sysPlcCfg = createSystemMemoryPolicy(dbCfg.getSystemCacheMemorySize());
+        MemoryPolicy memPlc = initMemory(dbCfg, memPlcCfg, memMetrics);
 
-        MemoryMetricsImpl sysMemMetrics = new MemoryMetricsImpl(sysPlcCfg);
+        memPlcMap.put(memPlcName, memPlc);
 
-        memPlcMap.put(SYSTEM_MEMORY_POLICY_NAME, initMemory(dbCfg, sysPlcCfg, sysMemMetrics));
+        memMetricsMap.put(memPlcName, memMetrics);
 
-        memMetricsMap.put(SYSTEM_MEMORY_POLICY_NAME, sysMemMetrics);
+        if (memPlcName.equals(dfltMemPlcName))
+            dfltMemPlc = memPlc;
+        else if (memPlcName.equals(DFLT_MEM_PLC_DEFAULT_NAME))
+            log.warning("Memory Policy with name 'default' isn't used as a default. " +
+                    "Please check Memory Policies configuration.");
     }
 
     /**
-     * @param memMetrics Mem metrics.
+     * @param memPlcsCfgs User-defined memory policy configurations.
      */
-    private void registerMetricsMBean(MemoryMetricsImpl memMetrics) {
-        IgniteConfiguration cfg = cctx.gridConfig();
-
-        try {
-            U.registerMBean(
-                    cfg.getMBeanServer(),
-                    cfg.getIgniteInstanceName(),
-                    "MemoryMetrics",
-                    memMetrics.getName(),
-                    memMetrics,
-                    MemoryMetricsMXBean.class);
-        }
-        catch (JMException e) {
-            log.warning("Failed to register MBean for MemoryMetrics with name: '" + memMetrics.getName()
+ "'");
+    private boolean hasCustomDefaultMemoryPolicy(MemoryPolicyConfiguration[] memPlcsCfgs)
{
+        for (MemoryPolicyConfiguration memPlcsCfg : memPlcsCfgs) {
+            if (DFLT_MEM_PLC_DEFAULT_NAME.equals(memPlcsCfg.getName()))
+                return true;
         }
+
+        return false;
     }
 
     /**
@@ -305,7 +331,7 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap
      * @throws IgniteCheckedException In case of validation violation.
      */
     private static void checkDefaultPolicyConfiguration(String dfltPlcName, Set<String>
plcNames) throws IgniteCheckedException {
-        if (dfltPlcName != null) {
+        if (!DFLT_MEM_PLC_DEFAULT_NAME.equals(dfltPlcName)) {
             if (dfltPlcName.isEmpty())
                 throw new IgniteCheckedException("User-defined default MemoryPolicy name
must be non-empty");
             if (!plcNames.contains(dfltPlcName))

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/MemoryPolicyConfigValidationTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/MemoryPolicyConfigValidationTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/MemoryPolicyConfigValidationTest.java
index c0f74d0..154e562 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/MemoryPolicyConfigValidationTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/MemoryPolicyConfigValidationTest.java
@@ -43,7 +43,7 @@ public class MemoryPolicyConfigValidationTest extends GridCommonAbstractTest
{
 
                 break;
 
-            case RESERVED_MEMORY_POLICY_MISUSE:
+            case SYSTEM_MEMORY_POLICY_NAME_MISUSE:
                 plcs = createPlcWithReservedNameMisuseCfg();
 
                 break;
@@ -147,7 +147,7 @@ public class MemoryPolicyConfigValidationTest extends GridCommonAbstractTest
{
      * 'sysMemPlc' name is reserved for MemoryPolicyConfiguration for system caches.
      */
     public void testReservedMemoryPolicyMisuse() throws Exception {
-        violationType = ValidationViolationType.RESERVED_MEMORY_POLICY_MISUSE;
+        violationType = ValidationViolationType.SYSTEM_MEMORY_POLICY_NAME_MISUSE;
 
         doTest(violationType);
     }
@@ -217,7 +217,7 @@ public class MemoryPolicyConfigValidationTest extends GridCommonAbstractTest
{
         NAMES_CONFLICT("Two MemoryPolicies have the same name: "),
 
         /** */
-        RESERVED_MEMORY_POLICY_MISUSE("'sysMemPlc' policy name is reserved for internal use."),
+        SYSTEM_MEMORY_POLICY_NAME_MISUSE("'sysMemPlc' policy name is reserved for internal
use."),
 
         /** */
         TOO_SMALL_MEMORY_SIZE("MemoryPolicy must have size more than 1MB: "),

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/database/MemoryPolicyInitializationTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/database/MemoryPolicyInitializationTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/database/MemoryPolicyInitializationTest.java
new file mode 100644
index 0000000..1e3f328
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/database/MemoryPolicyInitializationTest.java
@@ -0,0 +1,307 @@
+/*
+ * 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
+ *
+ *      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 org.apache.ignite.internal.processors.cache.database;
+
+import java.util.Collection;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.MemoryConfiguration;
+import org.apache.ignite.configuration.MemoryPolicyConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+import static org.apache.ignite.configuration.MemoryConfiguration.DFLT_MEM_PLC_DEFAULT_NAME;
+
+/**
+ *
+ */
+public class MemoryPolicyInitializationTest extends GridCommonAbstractTest {
+    /** */
+    private static final String CUSTOM_NON_DEFAULT_MEM_PLC_NAME = "custom_mem_plc";
+
+    /** */
+    private static final long USER_CUSTOM_MEM_PLC_SIZE = 10 * 1024 * 1024;
+
+    /** */
+    private static final long USER_DEFAULT_MEM_PLC_SIZE = 99 * 1024 * 1024;
+
+    /** */
+    private MemoryConfiguration memCfg;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws
Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setMemoryConfiguration(memCfg);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        memCfg = null;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /**
+     * Verifies that expected memory policies are allocated when used doesn't provide any
MemoryPolicyConfiguration.
+     */
+    public void testNoConfigProvided() throws Exception {
+        memCfg = null;
+
+        IgniteEx ignite = startGrid(0);
+
+        Collection<MemoryPolicy> allMemPlcs = ignite.context().cache().context().database().memoryPolicies();
+
+        assertTrue(allMemPlcs.size() == 2);
+
+        verifyDefaultAndSystemMemoryPolicies(allMemPlcs);
+    }
+
+    /**
+     * Verifies that expected memory policies are allocated when used provides MemoryPolicyConfiguration
+     * with non-default custom MemoryPolicy.
+     */
+    public void testCustomConfigNoDefault() throws Exception {
+        prepareCustomNoDefaultConfig();
+
+        IgniteEx ignite = startGrid(0);
+
+        Collection<MemoryPolicy> allMemPlcs = ignite.context().cache().context().database().memoryPolicies();
+
+        assertTrue(allMemPlcs.size() == 3);
+
+        verifyDefaultAndSystemMemoryPolicies(allMemPlcs);
+
+        assertTrue("Custom non-default memory policy is not presented",
+                isMemoryPolicyPresented(allMemPlcs, CUSTOM_NON_DEFAULT_MEM_PLC_NAME));
+    }
+
+    /**
+     * User is allowed to configure memory policy with 'default' name,
+     * in that case Ignite instance will use this user-defined memory policy as a default
one.
+     */
+    public void testCustomConfigOverridesDefault() throws Exception {
+        prepareCustomConfigWithOverridingDefault();
+
+        IgniteEx ignite = startGrid(0);
+
+        IgniteCacheDatabaseSharedManager dbMgr = ignite.context().cache().context().database();
+
+        Collection<MemoryPolicy> allMemPlcs = dbMgr.memoryPolicies();
+
+        assertTrue(allMemPlcs.size() == 2);
+
+        verifyDefaultAndSystemMemoryPolicies(allMemPlcs);
+
+        MemoryPolicy dfltMemPlc = U.field(dbMgr, "dfltMemPlc");
+
+        assertTrue(dfltMemPlc.config().getSize() == USER_DEFAULT_MEM_PLC_SIZE);
+    }
+
+    /**
+     * User is allowed to define fully custom memory policy and make it default by setting
its name to memory config.
+     *
+     * At the same time user still can create a memory policy with name 'default'
+     * which although won't be used as default.
+     */
+    public void testCustomConfigOverridesDefaultNameAndDeclaresDefault() throws Exception
{
+        prepareCustomConfigWithOverriddenDefaultName();
+
+        IgniteEx ignite = startGrid(0);
+
+        IgniteCacheDatabaseSharedManager dbMgr = ignite.context().cache().context().database();
+
+        Collection<MemoryPolicy> allMemPlcs = dbMgr.memoryPolicies();
+
+        assertTrue(allMemPlcs.size() == 3);
+
+        verifyDefaultAndSystemMemoryPolicies(allMemPlcs);
+
+        MemoryPolicy dfltMemPlc = U.field(dbMgr, "dfltMemPlc");
+
+        assertTrue(dfltMemPlc.config().getSize() == USER_CUSTOM_MEM_PLC_SIZE);
+    }
+
+    /**
+     * Test for verification that caches with not specified memory policy name,
+     * with specified default memory policy name and specified custom memory policy name
+     * all started with correct memory policy.
+     */
+    public void testCachesOnOverriddenMemoryPolicy() throws Exception {
+        prepareCustomConfigWithOverridingDefaultAndCustom();
+
+        IgniteEx ignite = startGrid(0);
+
+        CacheConfiguration cache1Cfg = new CacheConfiguration()
+                .setName("cache1");
+
+        IgniteCache cache1 = ignite.createCache(cache1Cfg);
+
+        verifyCacheMemoryPolicy(cache1, DFLT_MEM_PLC_DEFAULT_NAME);
+
+        CacheConfiguration cache2Cfg = new CacheConfiguration()
+                .setName("cache2")
+                .setMemoryPolicyName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        IgniteCache cache2 = ignite.createCache(cache2Cfg);
+
+        verifyCacheMemoryPolicy(cache2, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        CacheConfiguration cache3Cfg = new CacheConfiguration()
+                .setName("cache3")
+                .setMemoryPolicyName(DFLT_MEM_PLC_DEFAULT_NAME);
+
+        IgniteCache cache3 = ignite.createCache(cache3Cfg);
+
+        verifyCacheMemoryPolicy(cache3, DFLT_MEM_PLC_DEFAULT_NAME);
+    }
+
+    /**
+     * Test for verification that caches with not specified memory policy name,
+     * with specified default memory policy name and specified custom memory policy name
+     * all started with correct memory policy.
+     */
+    public void testCachesOnUserDefinedDefaultMemoryPolicy() throws Exception {
+        prepareCustomConfigWithOverriddenDefaultName();
+
+        IgniteEx ignite = startGrid(0);
+
+        CacheConfiguration cache1Cfg = new CacheConfiguration()
+                .setName("cache1");
+
+        IgniteCache cache1 = ignite.createCache(cache1Cfg);
+
+        verifyCacheMemoryPolicy(cache1, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        CacheConfiguration cache2Cfg = new CacheConfiguration()
+                .setName("cache2")
+                .setMemoryPolicyName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        IgniteCache cache2 = ignite.createCache(cache2Cfg);
+
+        verifyCacheMemoryPolicy(cache2, CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        CacheConfiguration cache3Cfg = new CacheConfiguration()
+                .setName("cache3")
+                .setMemoryPolicyName(DFLT_MEM_PLC_DEFAULT_NAME);
+
+        IgniteCache cache3 = ignite.createCache(cache3Cfg);
+
+        verifyCacheMemoryPolicy(cache3, DFLT_MEM_PLC_DEFAULT_NAME);
+    }
+
+    /**
+     * @param cache Cache.
+     * @param plcName Policy name.
+     */
+    private void verifyCacheMemoryPolicy(IgniteCache cache, String plcName) {
+        GridCacheContext ctx = U.field(cache, "ctx");
+
+        assertEquals(plcName, ctx.memoryPolicy().config().getName());
+    }
+
+    /**
+     *
+     */
+    private void prepareCustomConfigWithOverriddenDefaultName() {
+        memCfg = new MemoryConfiguration();
+
+        memCfg.setDefaultMemoryPolicyName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME);
+
+        memCfg.setMemoryPolicies(new MemoryPolicyConfiguration()
+                .setName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME)
+                .setSize(USER_CUSTOM_MEM_PLC_SIZE),
+
+                new MemoryPolicyConfiguration()
+                .setName(DFLT_MEM_PLC_DEFAULT_NAME)
+                .setSize(USER_DEFAULT_MEM_PLC_SIZE)
+        );
+    }
+
+
+    /**
+     *
+     */
+    private void prepareCustomConfigWithOverridingDefault() {
+        memCfg = new MemoryConfiguration();
+
+        memCfg.setMemoryPolicies(new MemoryPolicyConfiguration()
+                .setName(DFLT_MEM_PLC_DEFAULT_NAME)
+                .setSize(USER_DEFAULT_MEM_PLC_SIZE)
+        );
+    }
+
+    /**
+     *
+     */
+    private void prepareCustomConfigWithOverridingDefaultAndCustom() {
+        memCfg = new MemoryConfiguration();
+
+        memCfg.setMemoryPolicies(new MemoryPolicyConfiguration()
+                .setName(DFLT_MEM_PLC_DEFAULT_NAME)
+                .setSize(USER_DEFAULT_MEM_PLC_SIZE),
+
+                new MemoryPolicyConfiguration()
+                .setName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME)
+                .setSize(USER_CUSTOM_MEM_PLC_SIZE)
+        );
+    }
+
+    /**
+     * @param allMemPlcs Collection of all memory policies.
+     */
+    private void verifyDefaultAndSystemMemoryPolicies(Collection<MemoryPolicy> allMemPlcs)
{
+        assertTrue("Default memory policy is not presented",
+                isMemoryPolicyPresented(allMemPlcs, DFLT_MEM_PLC_DEFAULT_NAME));
+
+        assertTrue("System memory policy is not presented",
+                isMemoryPolicyPresented(allMemPlcs, IgniteCacheDatabaseSharedManager.SYSTEM_MEMORY_POLICY_NAME));
+    }
+
+    /**
+     *
+     */
+    private void prepareCustomNoDefaultConfig() {
+        memCfg = new MemoryConfiguration();
+
+        memCfg.setMemoryPolicies(new MemoryPolicyConfiguration()
+                .setName(CUSTOM_NON_DEFAULT_MEM_PLC_NAME)
+                .setSize(USER_CUSTOM_MEM_PLC_SIZE)
+        );
+    }
+
+    /**
+     * @param memPlcs Collection of memory policies.
+     * @param nameToVerify Excepted name of memory policy.
+     */
+    private boolean isMemoryPolicyPresented(Collection<MemoryPolicy> memPlcs, String
nameToVerify) {
+        for (MemoryPolicy memPlc : memPlcs) {
+            if (nameToVerify.equals(memPlc.config().getName()))
+                return true;
+        }
+
+        return false;
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
index 89e8f01..04a3753 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite2.java
@@ -44,6 +44,7 @@ import org.apache.ignite.internal.processors.cache.IgniteCachePartitionMapUpdate
 import org.apache.ignite.internal.processors.cache.IgniteDynamicCacheAndNodeStop;
 import org.apache.ignite.internal.processors.cache.IgniteOnePhaseCommitInvokeTest;
 import org.apache.ignite.internal.processors.cache.MemoryPolicyConfigValidationTest;
+import org.apache.ignite.internal.processors.cache.database.MemoryPolicyInitializationTest;
 import org.apache.ignite.internal.processors.cache.distributed.CacheLoadingConcurrentGridStartSelfTest;
 import org.apache.ignite.internal.processors.cache.distributed.CacheLoadingConcurrentGridStartSelfTestAllowOverwrite;
 import org.apache.ignite.internal.processors.cache.distributed.CacheLockReleaseNodeLeaveTest;
@@ -250,6 +251,7 @@ public class IgniteCacheTestSuite2 extends TestSuite {
         suite.addTest(new TestSuite(NearCacheSyncUpdateTest.class));
         suite.addTest(new TestSuite(CacheConfigurationLeakTest.class));
         suite.addTest(new TestSuite(MemoryPolicyConfigValidationTest.class));
+        suite.addTest(new TestSuite(MemoryPolicyInitializationTest.class));
         suite.addTest(new TestSuite(CacheMemoryPolicyConfigurationTest.class));
         suite.addTest(new TestSuite(CacheEnumOperationsSingleNodeTest.class));
         suite.addTest(new TestSuite(CacheEnumOperationsTest.class));

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryConfiguration.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryConfiguration.cs
b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryConfiguration.cs
index 9b1016d..9c4bb35 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryConfiguration.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryConfiguration.cs
@@ -56,12 +56,18 @@ namespace Apache.Ignite.Core.Cache.Configuration
         public const int DefaultPageSize = 2 * 1024;
 
         /// <summary>
+        /// The default value for <see cref="DefaultMemoryPolicyName"/>.
+        /// </summary>
+        public const string DefaultDefaultMemoryPolicyName = "default";
+
+        /// <summary>
         /// Initializes a new instance of the <see cref="MemoryConfiguration"/> class.
         /// </summary>
         public MemoryConfiguration()
         {
             SystemCacheMemorySize = DefaultSystemCacheMemorySize;
             PageSize = DefaultPageSize;
+            DefaultMemoryPolicyName = DefaultDefaultMemoryPolicyName;
         }
 
         /// <summary>
@@ -140,6 +146,7 @@ namespace Apache.Ignite.Core.Cache.Configuration
         /// <summary>
         /// Gets or sets the name of the default memory policy in <see cref="MemoryPolicies"/>.
         /// </summary>
+        [DefaultValue(DefaultDefaultMemoryPolicyName)]
         public string DefaultMemoryPolicyName { get; set; }
 
         /// <summary>

http://git-wip-us.apache.org/repos/asf/ignite/blob/1dc9e698/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryPolicyConfiguration.cs
----------------------------------------------------------------------
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryPolicyConfiguration.cs
b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryPolicyConfiguration.cs
index 9e21910..fe4e91f 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryPolicyConfiguration.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Cache/Configuration/MemoryPolicyConfiguration.cs
@@ -42,6 +42,7 @@ namespace Apache.Ignite.Core.Cache.Configuration
         {
             EvictionThreshold = DefaultEvictionThreshold;
             EmptyPagesPoolSize = DefaultEmptyPagesPoolSize;
+            Name = MemoryConfiguration.DefaultDefaultMemoryPolicyName;
         }
 
         /// <summary>
@@ -73,7 +74,9 @@ namespace Apache.Ignite.Core.Cache.Configuration
 
         /// <summary>
         /// Gets or sets the memory policy name.
+        /// Defaults to <see cref="MemoryConfiguration.DefaultDefaultMemoryPolicyName"/>.
         /// </summary>
+        [DefaultValue(MemoryConfiguration.DefaultDefaultMemoryPolicyName)]
         public string Name { get; set; }
 
         /// <summary>


Mime
View raw message