|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.processors.cache.persistence.db.file; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.OpenOption; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 25 | +import org.apache.ignite.configuration.DataRegionConfiguration; |
| 26 | +import org.apache.ignite.configuration.DataStorageConfiguration; |
| 27 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 28 | +import org.apache.ignite.configuration.WALMode; |
| 29 | +import org.apache.ignite.internal.IgniteEx; |
| 30 | +import org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager; |
| 31 | +import org.apache.ignite.internal.processors.cache.persistence.file.FileIO; |
| 32 | +import org.apache.ignite.internal.processors.cache.persistence.file.FileIODecorator; |
| 33 | +import org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory; |
| 34 | +import org.apache.ignite.internal.processors.cache.persistence.wal.FileWriteAheadLogManager; |
| 35 | +import org.apache.ignite.plugin.AbstractTestPluginProvider; |
| 36 | +import org.apache.ignite.plugin.PluginContext; |
| 37 | +import org.apache.ignite.testframework.GridTestUtils; |
| 38 | +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| 39 | +import org.jetbrains.annotations.Nullable; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.junit.runners.Parameterized; |
| 43 | + |
| 44 | +/** |
| 45 | + * Ckeck that WAL manager closes File IO interfaces. |
| 46 | + */ |
| 47 | +@RunWith(Parameterized.class) |
| 48 | +public class WalFilesCloseTest extends GridCommonAbstractTest { |
| 49 | + /** Opened File IO interfaces. */ |
| 50 | + private final List<FileIO> opened = new CopyOnWriteArrayList<>(); |
| 51 | + |
| 52 | + /** */ |
| 53 | + @Parameterized.Parameter |
| 54 | + public WALMode mode; |
| 55 | + |
| 56 | + /** */ |
| 57 | + @Parameterized.Parameters(name = "mode={0}") |
| 58 | + public static Object[] parameters() { |
| 59 | + return new Object[] {WALMode.FSYNC, WALMode.LOG_ONLY, WALMode.BACKGROUND}; |
| 60 | + } |
| 61 | + |
| 62 | + /** {@inheritDoc} */ |
| 63 | + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| 64 | + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); |
| 65 | + |
| 66 | + cfg.setDataStorageConfiguration(new DataStorageConfiguration() |
| 67 | + .setWalMode(mode) |
| 68 | + .setDefaultDataRegionConfiguration( |
| 69 | + new DataRegionConfiguration() |
| 70 | + .setPersistenceEnabled(true) |
| 71 | + )); |
| 72 | + |
| 73 | + cfg.setPluginProviders(new TestWalManagerProvider()); |
| 74 | + |
| 75 | + return cfg; |
| 76 | + } |
| 77 | + |
| 78 | + /** {@inheritDoc} */ |
| 79 | + @Override protected void afterTest() throws Exception { |
| 80 | + super.afterTest(); |
| 81 | + |
| 82 | + cleanPersistenceDir(); |
| 83 | + } |
| 84 | + |
| 85 | + /** */ |
| 86 | + @Test |
| 87 | + public void testStartStopServer() throws Exception { |
| 88 | + IgniteEx srv = startGrid(0); |
| 89 | + |
| 90 | + srv.close(); |
| 91 | + |
| 92 | + assertTrue(opened.isEmpty()); |
| 93 | + } |
| 94 | + |
| 95 | + /** Test class to track opened file IO interfaces. */ |
| 96 | + private class TestWalManagerProvider extends AbstractTestPluginProvider { |
| 97 | + /** {@inheritDoc} */ |
| 98 | + @Override public String name() { |
| 99 | + return "testPlugin"; |
| 100 | + } |
| 101 | + |
| 102 | + /** {@inheritDoc} */ |
| 103 | + @Override public <T> @Nullable T createComponent(PluginContext ctx, Class<T> cls) { |
| 104 | + if (!IgniteWriteAheadLogManager.class.equals(cls)) |
| 105 | + return null; |
| 106 | + |
| 107 | + FileWriteAheadLogManager wal = new FileWriteAheadLogManager(((IgniteEx)ctx.grid()).context()); |
| 108 | + |
| 109 | + FileIOFactory delegate = GridTestUtils.getFieldValue(wal, "ioFactory"); |
| 110 | + |
| 111 | + wal.setFileIOFactory(new FileIOFactory() { |
| 112 | + @Override public FileIO create(File file, OpenOption... modes) throws IOException { |
| 113 | + FileIODecorator fileIo = new FileIODecorator(delegate.create(file, modes)) { |
| 114 | + @Override public void close() throws IOException { |
| 115 | + super.close(); |
| 116 | + |
| 117 | + opened.remove(this); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + opened.add(fileIo); |
| 122 | + |
| 123 | + return fileIo; |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + return (T)wal; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments