Search in sources :

Example 1 with TimedSemaphore

use of org.apache.commons.lang3.concurrent.TimedSemaphore in project ozone by apache.

the class FollowerAppendLogEntryGenerator method call.

@Override
public Void call() throws Exception {
    inFlightMessages = new LinkedBlockingQueue<>(inflightLimit);
    timer = getMetrics().timer("append-entry");
    byte[] data = RandomStringUtils.randomAscii(chunkSize).getBytes(StandardCharsets.UTF_8);
    dataToWrite = ByteString.copyFrom(data);
    OzoneConfiguration conf = createOzoneConfiguration();
    setServerIdFromFile(conf);
    Preconditions.assertTrue(getThreadNo() == 1, "This test should be executed from one thread");
    // the raft identifier which is used by the freon
    requestor = RaftPeerProto.newBuilder().setId(RaftPeerId.valueOf(FAKE_LEADER_ID).toByteString()).setAddress(FAKE_LEADER_ADDDRESS).build();
    NettyChannelBuilder channelBuilder = NettyChannelBuilder.forTarget(serverAddress);
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
    ManagedChannel build = channelBuilder.build();
    stub = RaftServerProtocolServiceGrpc.newStub(build);
    if (rateLimit != 0) {
        rateLimiter = new TimedSemaphore(1, TimeUnit.SECONDS, rateLimit);
    }
    init();
    sender = stub.appendEntries(this);
    if (nextIndex == 0) {
        // first: configure a new ratis group (one follower, one fake leader
        // (freon))
        configureGroup();
        RequestVoteReplyProto vote = requestVote().get(1000, TimeUnit.SECONDS);
        LOG.info("Datanode answered to the vote request: {}", vote);
        if (!vote.getServerReply().getSuccess()) {
            throw new RuntimeException("Datanode didn't vote to the fake freon leader.");
        }
        // send the first appendEntry. This one is special as it initialized the
        // log.
        long callId = callIdRandom.nextLong();
        inFlightMessages.put(callId);
        sender.onNext(createInitialLogEntry(callId));
        nextIndex = 1L;
    }
    // We can generate as mach entry as we need.
    runTests(this::sendAppendLogEntryRequest);
    if (rateLimiter != null) {
        rateLimiter.shutdown();
    }
    return null;
}
Also used : OzoneConfiguration(org.apache.hadoop.hdds.conf.OzoneConfiguration) NettyChannelBuilder(org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder) ManagedChannel(org.apache.ratis.thirdparty.io.grpc.ManagedChannel) RequestVoteReplyProto(org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto) TimedSemaphore(org.apache.commons.lang3.concurrent.TimedSemaphore)

Example 2 with TimedSemaphore

use of org.apache.commons.lang3.concurrent.TimedSemaphore in project ReplicaDB by osalvador.

the class SqlManager method bandwidthThrottlingCreate.

/**
 * Create a bandwith cap, estimating the size of the first row returned by the resultset
 * and using it as permits in the rate limit.
 *
 * @param resultSet the resultset cursor moved to the first row (resultSet.next())
 * @param rsmd      the result set metadata object
 * @throws SQLException
 */
@Deprecated
protected void bandwidthThrottlingCreate(ResultSet resultSet, ResultSetMetaData rsmd) throws SQLException {
    int kilobytesPerSecond = options.getBandwidthThrottling();
    if (kilobytesPerSecond > 0) {
        // Stimate the Row Size
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            if (rsmd.getColumnType(i) != Types.BLOB) {
                String columnValue = resultSet.getString(i);
                if (columnValue != null && !resultSet.getString(i).isEmpty())
                    rowSize = rowSize + resultSet.getString(i).length();
            }
        }
        double limit = ((1.0 * kilobytesPerSecond) / rowSize) / (options.getFetchSize() * 1.0 / 1000);
        if (limit == 0)
            limit = 1;
        this.bandwidthRateLimiter = new TimedSemaphore(1, TimeUnit.SECONDS, (int) Math.round(limit));
        LOG.info("Estimated Row Size: {} KB. Estimated limit of fetchs per second: {} ", rowSize, limit);
    }
}
Also used : TimedSemaphore(org.apache.commons.lang3.concurrent.TimedSemaphore)

Aggregations

TimedSemaphore (org.apache.commons.lang3.concurrent.TimedSemaphore)2 OzoneConfiguration (org.apache.hadoop.hdds.conf.OzoneConfiguration)1 RequestVoteReplyProto (org.apache.ratis.proto.RaftProtos.RequestVoteReplyProto)1 ManagedChannel (org.apache.ratis.thirdparty.io.grpc.ManagedChannel)1 NettyChannelBuilder (org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder)1