IGNITE-5067 - Fixed absolute swap file path handling for memory policy configuration. Fixes #1867
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6f749bf4 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6f749bf4 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6f749bf4 Branch: refs/heads/ignite-5757 Commit: 6f749bf4bebb135250a3f99923ed87b4b7d0c29f Parents: c1a3b37 Author: Sergey Chugunov <[email protected]> Authored: Fri Jul 21 17:59:10 2017 +0300 Committer: Alexey Goncharuk <[email protected]> Committed: Fri Jul 21 18:00:40 2017 +0300 ---------------------------------------------------------------------- .../IgniteCacheDatabaseSharedManager.java | 22 ++- .../database/SwapPathConstructionSelfTest.java | 157 +++++++++++++++++++ .../ignite/testsuites/IgniteBasicTestSuite.java | 3 + 3 files changed, 174 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/6f749bf4/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java index eec3b85..e07c51e 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/IgniteCacheDatabaseSharedManager.java @@ -25,7 +25,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import javax.management.JMException; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.MemoryMetrics; @@ -204,8 +203,9 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap /** * @param memCfg Database config. + * @throws IgniteCheckedException If failed to initialize swap path. */ - protected void initPageMemoryPolicies(MemoryConfiguration memCfg) { + protected void initPageMemoryPolicies(MemoryConfiguration memCfg) throws IgniteCheckedException { MemoryPolicyConfiguration[] memPlcsCfgs = memCfg.getMemoryPolicies(); if (memPlcsCfgs == null) { @@ -261,12 +261,13 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap * @param memCfg Database config. * @param memPlcCfg Memory policy config. * @param memPlcName Memory policy name. + * @throws IgniteCheckedException If failed to initialize swap path. */ private void addMemoryPolicy( MemoryConfiguration memCfg, MemoryPolicyConfiguration memPlcCfg, String memPlcName - ) { + ) throws IgniteCheckedException { String dfltMemPlcName = memCfg.getDefaultMemoryPolicyName(); if (dfltMemPlcName == null) @@ -844,12 +845,14 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap * @param plcCfg memory policy with PageMemory specific parameters. * @param memMetrics {@link MemoryMetrics} object to collect memory usage metrics. * @return Memory policy instance. + * + * @throws IgniteCheckedException If failed to initialize swap path. */ private MemoryPolicy initMemory( MemoryConfiguration memCfg, MemoryPolicyConfiguration plcCfg, MemoryMetricsImpl memMetrics - ) { + ) throws IgniteCheckedException { File allocPath = buildAllocPath(plcCfg); DirectMemoryProvider memProvider = allocPath == null ? @@ -892,8 +895,10 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap * Builds allocation path for memory mapped file to be used with PageMemory. * * @param plc MemoryPolicyConfiguration. + * + * @throws IgniteCheckedException If resolving swap directory fails. */ - @Nullable protected File buildAllocPath(MemoryPolicyConfiguration plc) { + @Nullable protected File buildAllocPath(MemoryPolicyConfiguration plc) throws IgniteCheckedException { String path = plc.getSwapFilePath(); if (path == null) @@ -938,13 +943,14 @@ public class IgniteCacheDatabaseSharedManager extends GridCacheSharedManagerAdap * @param path Path to the working directory. * @param consId Consistent ID of the local node. * @return DB storage path. + * + * @throws IgniteCheckedException If resolving swap directory fails. */ - protected File buildPath(String path, String consId) { + protected File buildPath(String path, String consId) throws IgniteCheckedException { String igniteHomeStr = U.getIgniteHome(); - File igniteHome = igniteHomeStr != null ? new File(igniteHomeStr) : null; + File workDir = igniteHomeStr == null ? new File(path) : U.resolveWorkDirectory(igniteHomeStr, path, false); - File workDir = igniteHome == null ? new File(path) : new File(igniteHome, path); return new File(workDir, consId); } http://git-wip-us.apache.org/repos/asf/ignite/blob/6f749bf4/modules/core/src/test/java/org/apache/ignite/internal/processors/database/SwapPathConstructionSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/database/SwapPathConstructionSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/SwapPathConstructionSelfTest.java new file mode 100644 index 0000000..53e5daf --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/database/SwapPathConstructionSelfTest.java @@ -0,0 +1,157 @@ +/* + * 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.database; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.MemoryConfiguration; +import org.apache.ignite.configuration.MemoryPolicyConfiguration; +import org.apache.ignite.internal.GridKernalContext; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.pagemem.PageMemory; +import org.apache.ignite.internal.processors.cache.persistence.IgniteCacheDatabaseSharedManager; +import org.apache.ignite.internal.processors.cache.persistence.MemoryPolicy; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * Test verifies correct construction of swap file path {@link MemoryPolicyConfiguration#setSwapFilePath(String)} + * when absolute or relative paths are provided via configuration. + */ +public class SwapPathConstructionSelfTest extends GridCommonAbstractTest { + /** */ + private MemoryConfiguration memCfg; + + /** */ + private static final String RELATIVE_SWAP_PATH = "relSwapPath"; + + /** */ + private static final String ABSOLUTE_SWAP_PATH = "absoluteSwapPath"; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + cfg.setMemoryConfiguration(memCfg); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + cleanUpSwapDir(); + } + + /** + * Cleans up swap files and directories after test. + */ + private void cleanUpSwapDir() { + Path relDir = Paths.get(U.getIgniteHome(), RELATIVE_SWAP_PATH); + + deleteRecursively(relDir.toFile()); + + Path absDir = Paths.get(getTmpDir(), ABSOLUTE_SWAP_PATH); + + deleteRecursively(absDir.toFile()); + } + + /** + * Verifies relative swap file path construction. Directory with swap files is cleaned up during after-test phase. + */ + public void testRelativeSwapFilePath() throws Exception { + memCfg = createMemoryConfiguration(true); + + IgniteEx ignite = startGrid(0); + + String allocPath = extractDefaultPageMemoryAllocPath(ignite.context()); + + assertNotNull(allocPath); + + assertTrue(allocPath.contains(Paths.get(U.getIgniteHome(), RELATIVE_SWAP_PATH).toString())); + } + + /** + * Verifies absolute swap file path construction. System tmp directory is used to allocate swap files, + * so no clean up is needed. + */ + public void testAbsoluteSwapFilePath() throws Exception { + memCfg = createMemoryConfiguration(false); + + IgniteEx ignite = startGrid(0); + + String allocPath = extractDefaultPageMemoryAllocPath(ignite.context()); + + assertNotNull(allocPath); + + String expectedPath = Paths.get(getTmpDir(), ABSOLUTE_SWAP_PATH).toString(); + + assertTrue("Expected path: " + + expectedPath + + "; actual path: " + + allocPath, + allocPath.startsWith(expectedPath)); + } + + /** + * @param context Context. + */ + private String extractDefaultPageMemoryAllocPath(GridKernalContext context) { + IgniteCacheDatabaseSharedManager dbMgr = context.cache().context().database(); + + Map<String, MemoryPolicy> memPlcMap = U.field(dbMgr, "memPlcMap"); + + PageMemory pageMem = memPlcMap.get("default").pageMemory(); + + Object memProvider = U.field(pageMem, "directMemoryProvider"); + + return ((File) U.field(memProvider, "allocationPath")).getAbsolutePath(); + } + + /** + * @param isRelativePath flag is set to {@code true} if relative path should be used for memory policy configuration. + */ + private MemoryConfiguration createMemoryConfiguration(boolean isRelativePath) { + MemoryConfiguration memCfg = new MemoryConfiguration(); + + MemoryPolicyConfiguration memPlcCfg = new MemoryPolicyConfiguration(); + + memPlcCfg.setName("default"); + memPlcCfg.setMaxSize(20 * 1024 * 1024); + + if (isRelativePath) + memPlcCfg.setSwapFilePath(RELATIVE_SWAP_PATH); + else + memPlcCfg.setSwapFilePath(Paths.get(getTmpDir(), ABSOLUTE_SWAP_PATH).toString()); + + memCfg.setMemoryPolicies(memPlcCfg); + + return memCfg; + } + + /** + * + */ + private String getTmpDir() { + return System.getProperty("java.io.tmpdir"); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/6f749bf4/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java index d79e868..2ec2c74 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBasicTestSuite.java @@ -20,6 +20,8 @@ package org.apache.ignite.testsuites; import java.util.Set; import junit.framework.TestSuite; import org.apache.ignite.GridSuppressedExceptionSelfTest; +import org.apache.ignite.internal.processors.database.SwapPathConstructionSelfTest; +import org.apache.ignite.util.AttributeNodeFilterSelfTest; import org.apache.ignite.internal.ClusterGroupHostsSelfTest; import org.apache.ignite.internal.ClusterGroupSelfTest; import org.apache.ignite.internal.GridFailFastNodeFailureDetectionSelfTest; @@ -173,6 +175,7 @@ public class IgniteBasicTestSuite extends TestSuite { suite.addTestSuite(MetadataStorageSelfTest.class); suite.addTestSuite(FreeListImplSelfTest.class); suite.addTestSuite(MemoryMetricsSelfTest.class); + suite.addTestSuite(SwapPathConstructionSelfTest.class); suite.addTestSuite(IgniteMarshallerCacheFSRestoreTest.class); suite.addTestSuite(IgniteMarshallerCacheClassNameConflictTest.class);
