ctubbsii commented on a change in pull request #2181: URL: https://github.com/apache/accumulo/pull/2181#discussion_r669076996
########## File path: server/tserver/src/test/java/org/apache/accumulo/tserver/log/RecoveryLogsIteratorTest.java ########## @@ -0,0 +1,267 @@ +/* + * 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.accumulo.tserver.log; + +import static org.apache.accumulo.tserver.logger.LogEvents.DEFINE_TABLET; +import static org.apache.accumulo.tserver.logger.LogEvents.OPEN; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.TreeMap; + +import org.apache.accumulo.core.conf.DefaultConfiguration; +import org.apache.accumulo.core.crypto.CryptoServiceFactory; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.dataImpl.KeyExtent; +import org.apache.accumulo.core.util.Pair; +import org.apache.accumulo.server.ServerContext; +import org.apache.accumulo.server.fs.VolumeManager; +import org.apache.accumulo.server.fs.VolumeManagerImpl; +import org.apache.accumulo.server.log.SortedLogState; +import org.apache.accumulo.tserver.logger.LogFileKey; +import org.apache.accumulo.tserver.logger.LogFileValue; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "paths not set by user input") +public class RecoveryLogsIteratorTest { + + private VolumeManager fs; + private File workDir; + static final KeyExtent extent = new KeyExtent(TableId.of("table"), null, null); + static ServerContext context; + static LogSorter logSorter; + + @Rule + public TemporaryFolder tempFolder = + new TemporaryFolder(new File(System.getProperty("user.dir") + "/target")); + + @Before + public void setUp() throws Exception { + context = createMock(ServerContext.class); + logSorter = new LogSorter(context, DefaultConfiguration.getInstance()); + + workDir = tempFolder.newFolder(); + String path = workDir.getAbsolutePath(); + assertTrue(workDir.delete()); + fs = VolumeManagerImpl.getLocalForTesting(path); + expect(context.getVolumeManager()).andReturn(fs).anyTimes(); + expect(context.getCryptoService()).andReturn(CryptoServiceFactory.newDefaultInstance()) + .anyTimes(); + expect(context.getConfiguration()).andReturn(DefaultConfiguration.getInstance()).anyTimes(); + replay(context); + } + + @After + public void tearDown() throws Exception { + fs.close(); + } + + static class KeyValue implements Comparable<KeyValue> { + public final LogFileKey key; + public final LogFileValue value; + + KeyValue() { + key = new LogFileKey(); + value = new LogFileValue(); + } + + @Override + public int hashCode() { + return Objects.hashCode(key) + Objects.hashCode(value); + } + + @Override + public boolean equals(Object obj) { + return this == obj || (obj instanceof KeyValue && 0 == compareTo((KeyValue) obj)); + } + + @Override + public int compareTo(KeyValue o) { + return key.compareTo(o.key); + } + } + + @Test + public void testSimpleRLI() throws IOException { + KeyValue keyValue = new KeyValue(); + keyValue.key.event = DEFINE_TABLET; + keyValue.key.seq = 0; + keyValue.key.tabletId = 1; + keyValue.key.tablet = extent; + + KeyValue[] keyValues = {keyValue}; + + Map<String,KeyValue[]> logs = new TreeMap<>(); + logs.put("keyValues", keyValues); + + ArrayList<Path> dirs = new ArrayList<>(); + + createRecoveryDir(logs, dirs, true); + + try (RecoveryLogsIterator rli = new RecoveryLogsIterator(context, dirs, null, null, false)) { + while (rli.hasNext()) { + Entry<LogFileKey,LogFileValue> entry = rli.next(); + assertEquals("TabletId does not match", 1, entry.getKey().tabletId); + assertEquals("Event does not match", DEFINE_TABLET, entry.getKey().event); + } + } + } + + @Test + public void testFinishMarker() throws IOException { + KeyValue keyValue = new KeyValue(); + keyValue.key.event = DEFINE_TABLET; + keyValue.key.seq = 0; + keyValue.key.tabletId = 1; + keyValue.key.tablet = extent; + + KeyValue[] keyValues = {keyValue}; + + Map<String,KeyValue[]> logs = new TreeMap<>(); + logs.put("keyValues", keyValues); + + ArrayList<Path> dirs = new ArrayList<>(); + + createRecoveryDir(logs, dirs, false); + + try (RecoveryLogsIterator rli = new RecoveryLogsIterator(context, dirs, null, null, false)) { + while (rli.hasNext()) { + fail("Finish marker should not be found. Exception should have been thrown."); + } + } catch (IOException e) { + // Expected exception + } + } Review comment: It may be possible to use assertThrows here, something like: ```suggestion assertThrows("Finish marker should not be found", IOException.class, () -> new RecoveryLogsIterator(context, dirs, null, null, false)); } ``` This code would be slightly different if the expected exception is thrown in the `hasNext()` method instead of in the constructor, but the idea is the same. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
