|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.asterix.app.nc; |
| 20 | + |
| 21 | +import java.io.BufferedWriter; |
| 22 | +import java.io.File; |
| 23 | +import java.io.FilenameFilter; |
| 24 | +import java.io.IOException; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.nio.file.Paths; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Comparator; |
| 30 | +import java.util.List; |
| 31 | +import java.util.logging.Level; |
| 32 | +import java.util.logging.Logger; |
| 33 | + |
| 34 | +import org.apache.asterix.common.storage.IIndexCheckpointManager; |
| 35 | +import org.apache.asterix.common.storage.IndexCheckpoint; |
| 36 | +import org.apache.asterix.common.utils.StorageConstants; |
| 37 | +import org.apache.hyracks.api.exceptions.HyracksDataException; |
| 38 | +import org.apache.hyracks.util.annotations.ThreadSafe; |
| 39 | + |
| 40 | +@ThreadSafe |
| 41 | +public class IndexCheckpointManager implements IIndexCheckpointManager { |
| 42 | + |
| 43 | + private static final Logger LOGGER = Logger.getLogger(IndexCheckpointManager.class.getName()); |
| 44 | + private static final int HISTORY_CHECKPOINTS = 1; |
| 45 | + private static final int MAX_CHECKPOINT_WRITE_ATTEMPTS = 5; |
| 46 | + private static final FilenameFilter CHECKPOINT_FILE_FILTER = |
| 47 | + (file, name) -> name.startsWith(StorageConstants.INDEX_CHECKPOINT_FILE_PREFIX); |
| 48 | + private static final long BULKLOAD_LSN = 0; |
| 49 | + private final Path indexPath; |
| 50 | + |
| 51 | + public IndexCheckpointManager(Path indexPath) { |
| 52 | + this.indexPath = indexPath; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public synchronized void init(long lsn) throws HyracksDataException { |
| 57 | + final List<IndexCheckpoint> checkpoints = getCheckpoints(); |
| 58 | + if (!checkpoints.isEmpty()) { |
| 59 | + LOGGER.warning(() -> "Checkpoints found on initializing: " + indexPath); |
| 60 | + delete(); |
| 61 | + } |
| 62 | + IndexCheckpoint firstCheckpoint = IndexCheckpoint.first(lsn); |
| 63 | + persist(firstCheckpoint); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public synchronized void replicated(String componentTimestamp, long masterLsn) throws HyracksDataException { |
| 68 | + final Long localLsn = getLatest().getMasterNodeFlushMap().get(masterLsn); |
| 69 | + if (localLsn == null) { |
| 70 | + throw new IllegalStateException("Component flushed before lsn mapping was received"); |
| 71 | + } |
| 72 | + flushed(componentTimestamp, localLsn); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public synchronized void flushed(String componentTimestamp, long lsn) throws HyracksDataException { |
| 77 | + final IndexCheckpoint latest = getLatest(); |
| 78 | + IndexCheckpoint nextCheckpoint = IndexCheckpoint.next(latest, lsn, componentTimestamp); |
| 79 | + persist(nextCheckpoint); |
| 80 | + deleteHistory(nextCheckpoint.getId(), HISTORY_CHECKPOINTS); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public synchronized void masterFlush(long masterLsn, long localLsn) throws HyracksDataException { |
| 85 | + final IndexCheckpoint latest = getLatest(); |
| 86 | + latest.getMasterNodeFlushMap().put(masterLsn, localLsn); |
| 87 | + final IndexCheckpoint next = |
| 88 | + IndexCheckpoint.next(latest, latest.getLowWatermark(), latest.getValidComponentTimestamp()); |
| 89 | + persist(next); |
| 90 | + notifyAll(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public synchronized long getLowWatermark() throws HyracksDataException { |
| 95 | + return getLatest().getLowWatermark(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public synchronized boolean isFlushed(long masterLsn) throws HyracksDataException { |
| 100 | + if (masterLsn == BULKLOAD_LSN) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + return getLatest().getMasterNodeFlushMap().containsKey(masterLsn); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public synchronized void advanceLowWatermark(long lsn) throws HyracksDataException { |
| 108 | + flushed(getLatest().getValidComponentTimestamp(), lsn); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public synchronized void delete() { |
| 113 | + deleteHistory(Long.MAX_VALUE, 0); |
| 114 | + } |
| 115 | + |
| 116 | + private IndexCheckpoint getLatest() { |
| 117 | + final List<IndexCheckpoint> checkpoints = getCheckpoints(); |
| 118 | + if (checkpoints.isEmpty()) { |
| 119 | + throw new IllegalStateException("Couldn't find any checkpoints for resource: " + indexPath); |
| 120 | + } |
| 121 | + checkpoints.sort(Comparator.comparingLong(IndexCheckpoint::getId).reversed()); |
| 122 | + return checkpoints.get(0); |
| 123 | + } |
| 124 | + |
| 125 | + private List<IndexCheckpoint> getCheckpoints() { |
| 126 | + List<IndexCheckpoint> checkpoints = new ArrayList<>(); |
| 127 | + final File[] checkpointFiles = indexPath.toFile().listFiles(CHECKPOINT_FILE_FILTER); |
| 128 | + if (checkpointFiles != null) { |
| 129 | + for (File checkpointFile : checkpointFiles) { |
| 130 | + try { |
| 131 | + checkpoints.add(read(checkpointFile.toPath())); |
| 132 | + } catch (IOException e) { |
| 133 | + LOGGER.log(Level.WARNING, e, () -> "Couldn't read index checkpoint file: " + e); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + return checkpoints; |
| 138 | + } |
| 139 | + |
| 140 | + private void persist(IndexCheckpoint checkpoint) throws HyracksDataException { |
| 141 | + final Path checkpointPath = getCheckpointPath(checkpoint); |
| 142 | + for (int i = 1; i <= MAX_CHECKPOINT_WRITE_ATTEMPTS; i++) { |
| 143 | + try { |
| 144 | + // clean up from previous write failure |
| 145 | + if (checkpointPath.toFile().exists()) { |
| 146 | + Files.delete(checkpointPath); |
| 147 | + } |
| 148 | + try (BufferedWriter writer = Files.newBufferedWriter(checkpointPath)) { |
| 149 | + writer.write(checkpoint.asJson()); |
| 150 | + } |
| 151 | + // ensure it was written correctly by reading it |
| 152 | + read(checkpointPath); |
| 153 | + } catch (IOException e) { |
| 154 | + if (i == MAX_CHECKPOINT_WRITE_ATTEMPTS) { |
| 155 | + throw HyracksDataException.create(e); |
| 156 | + } |
| 157 | + LOGGER.log(Level.WARNING, e, () -> "Filed to write checkpoint at: " + indexPath); |
| 158 | + int nextAttempt = i + 1; |
| 159 | + LOGGER.info(() -> "Checkpoint write attempt " + nextAttempt + "/" + MAX_CHECKPOINT_WRITE_ATTEMPTS); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private IndexCheckpoint read(Path checkpointPath) throws IOException { |
| 165 | + return IndexCheckpoint.fromJson(new String(Files.readAllBytes(checkpointPath))); |
| 166 | + } |
| 167 | + |
| 168 | + private void deleteHistory(long latestId, int historyToKeep) { |
| 169 | + try { |
| 170 | + final File[] checkpointFiles = indexPath.toFile().listFiles(CHECKPOINT_FILE_FILTER); |
| 171 | + if (checkpointFiles != null) { |
| 172 | + for (File checkpointFile : checkpointFiles) { |
| 173 | + if (getCheckpointIdFromFileName(checkpointFile.toPath()) < (latestId - historyToKeep)) { |
| 174 | + Files.delete(checkpointFile.toPath()); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } catch (Exception e) { |
| 179 | + LOGGER.log(Level.WARNING, e, () -> "Couldn't delete history checkpoints at " + indexPath); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private Path getCheckpointPath(IndexCheckpoint checkpoint) { |
| 184 | + return Paths.get(indexPath.toString(), |
| 185 | + StorageConstants.INDEX_CHECKPOINT_FILE_PREFIX + String.valueOf(checkpoint.getId())); |
| 186 | + } |
| 187 | + |
| 188 | + private long getCheckpointIdFromFileName(Path checkpointPath) { |
| 189 | + return Long.valueOf(checkpointPath.getFileName().toString() |
| 190 | + .substring(StorageConstants.INDEX_CHECKPOINT_FILE_PREFIX.length())); |
| 191 | + } |
| 192 | +} |
0 commit comments