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

apache / iotdb / #10029

07 Sep 2023 05:31PM UTC coverage: 47.638% (+0.008%) from 47.63%
#10029

push

travis_ci

web-flow
Pipe: Clear reference count of on-the-fly EnrichedEvent in queues when pipe is dropped (#11077) (#11087)

Co-authored-by: Steve Yurong Su <rong@apache.org>
(cherry picked from commit c4ff2b134)

Co-authored-by: 马子坤 <55695098+DanielWang2035@users.noreply.github.com>

57 of 57 new or added lines in 8 files covered. (100.0%)

80267 of 168493 relevant lines covered (47.64%)

0.48 hits per line

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

78.95
/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.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.rpc;
21

22
import java.util.Arrays;
23

24
/**
25
 * Helper class that wraps a byte[] so that it can expand and be reused. Users should call
26
 * resizeIfNecessary to make sure the buffer has suitable capacity, and then use the array as
27
 * needed.
28
 *
29
 * <p>Resizing policies: Expanding: If the required size > current capacity * 1.5, expand to the
30
 * required size, otherwise expand to current capacity * 1.5. Shrinking: If initial size < the
31
 * required size < current capacity * 0.6, and such small requests last for more than 5 times,
32
 * shrink to the middle of the required size and current capacity.
33
 */
34
class AutoResizingBuffer {
35

36
  private byte[] array;
37
  private int bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME;
1✔
38
  private final int initialCapacity;
39
  private long lastShrinkTime;
40

41
  public AutoResizingBuffer(int initialCapacity) {
1✔
42
    this.array = new byte[initialCapacity];
1✔
43
    this.initialCapacity = initialCapacity;
1✔
44
  }
1✔
45

46
  public void resizeIfNecessary(int size) {
47
    final int currentCapacity = this.array.length;
1✔
48
    final double loadFactor = 0.6;
1✔
49
    if (currentCapacity < size) {
1✔
50
      // Increase by a factor of 1.5x
51
      int growCapacity = currentCapacity + (currentCapacity >> 1);
1✔
52
      int newCapacity = Math.max(growCapacity, size);
1✔
53
      this.array = Arrays.copyOf(array, newCapacity);
1✔
54
      bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME;
1✔
55
    } else if (size > initialCapacity
1✔
56
        && currentCapacity * loadFactor > size
57
        && bufTooLargeCounter-- <= 0
58
        && System.currentTimeMillis() - lastShrinkTime > RpcUtils.MIN_SHRINK_INTERVAL) {
×
59
      // do not resize if it is reading the request size and do not shrink too often
60
      array = Arrays.copyOf(array, size + (currentCapacity - size) / 2);
×
61
      bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME;
×
62
      lastShrinkTime = System.currentTimeMillis();
×
63
    }
64
  }
1✔
65

66
  public byte[] array() {
67
    return this.array;
1✔
68
  }
69
}
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