• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10032

08 Sep 2023 06:40AM UTC coverage: 47.635% (-0.003%) from 47.638%
#10032

push

travis_ci

web-flow
[To rel/1.2] Remove some copyright info (#11096)

80288 of 168549 relevant lines covered (47.63%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/utils/RatisLogMonitor.java
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

20
package org.apache.iotdb.consensus.ratis.utils;
21

22
import org.slf4j.Logger;
23
import org.slf4j.LoggerFactory;
24

25
import java.io.File;
26
import java.io.IOException;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.util.Collections;
30
import java.util.HashMap;
31
import java.util.Set;
32
import java.util.function.Predicate;
33
import java.util.stream.Collectors;
34
import java.util.stream.Stream;
35

36
/**
37
 * Monitoring Ratis RaftLog total size. It will memorize the state of all files in the last update
38
 * and calculates the diff incrementally each run.
39
 */
40
public class RatisLogMonitor {
1✔
41
  private static final Logger logger = LoggerFactory.getLogger(RatisLogMonitor.class);
1✔
42

43
  /* whether the path denotes an open segment under active writing progress */
44
  private static final Predicate<Path> isOpenSegment =
1✔
45
      p -> p.toFile().getName().startsWith("log_inprogress");
1✔
46

47
  private static final class DirectoryState {
1✔
48
    private long size = 0;
1✔
49
    private Set<Path> memorizedFiles = Collections.emptySet();
1✔
50

51
    private void update(long size, Set<Path> latest) {
52
      this.size = size;
1✔
53
      this.memorizedFiles = latest;
1✔
54
    }
1✔
55
  }
56

57
  private final HashMap<File, DirectoryState> directoryMap = new HashMap<>();
1✔
58

59
  public long updateAndGetDirectorySize(File dir) {
60
    final DirectoryState state = directoryMap.computeIfAbsent(dir, d -> new DirectoryState());
1✔
61
    Set<Path> latest;
62
    try (final Stream<Path> files = Files.list(dir.toPath())) {
1✔
63
      latest = files.filter(isOpenSegment).collect(Collectors.toSet());
1✔
64
    } catch (IOException e) {
×
65
      logger.warn("{}: Error caught when listing files under {}:", this, dir, e);
×
66
      // keep the files unchanged and return the size calculated last time
67
      return state.size;
×
68
    }
1✔
69
    final long sizeDiff = diff(state.memorizedFiles, latest);
1✔
70
    final long newSize = state.size + sizeDiff;
1✔
71
    state.update(newSize, latest);
1✔
72
    return newSize;
1✔
73
  }
74

75
  public Set<Path> getFilesUnder(File dir) {
76
    return Collections.unmodifiableSet(directoryMap.get(dir).memorizedFiles);
1✔
77
  }
78

79
  private static long diff(Set<Path> old, Set<Path> latest) {
80
    final long incremental = totalSize(latest.stream().filter(p -> !old.contains(p)));
1✔
81
    final long decremental = totalSize(old.stream().filter(p -> !latest.contains(p)));
1✔
82
    return incremental - decremental;
1✔
83
  }
84

85
  private static long totalSize(Stream<Path> files) {
86
    return files.mapToLong(p -> p.toFile().length()).sum();
1✔
87
  }
88
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc