All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.accumulo.tserver.tablet.TabletMemory Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * 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.tablet;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.List;

import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.sample.impl.SamplerConfigurationImpl;
import org.apache.accumulo.server.ServerContext;
import org.apache.accumulo.tserver.InMemoryMap;
import org.apache.accumulo.tserver.InMemoryMap.MemoryIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class TabletMemory implements Closeable {
  private static final Logger log = LoggerFactory.getLogger(TabletMemory.class);

  private final Tablet tablet;
  private InMemoryMap memTable;
  private InMemoryMap otherMemTable;
  private InMemoryMap deletingMemTable;
  private long nextSeq = 1L;
  private CommitSession commitSession;
  private ServerContext context;

  TabletMemory(Tablet tablet) {
    this.tablet = tablet;
    this.context = tablet.getContext();
    memTable =
        new InMemoryMap(tablet.getTableConfiguration(), context, tablet.getExtent().getTableId());
    commitSession = new CommitSession(tablet, nextSeq, memTable);
    nextSeq += 2;
  }

  public InMemoryMap getMemTable() {
    return memTable;
  }

  public InMemoryMap getMinCMemTable() {
    return otherMemTable;
  }

  public CommitSession prepareForMinC() {
    if (otherMemTable != null) {
      throw new IllegalStateException();
    }

    if (deletingMemTable != null) {
      throw new IllegalStateException();
    }
    if (commitSession == null) {
      throw new IllegalStateException();
    }

    otherMemTable = memTable;
    memTable =
        new InMemoryMap(tablet.getTableConfiguration(), context, tablet.getExtent().getTableId());

    CommitSession oldCommitSession = commitSession;
    commitSession = new CommitSession(tablet, nextSeq, memTable);
    nextSeq += 2;

    tablet.updateMemoryUsageStats(memTable.estimatedSizeInBytes(),
        otherMemTable.estimatedSizeInBytes());

    return oldCommitSession;
  }

  public void finishedMinC() {

    if (otherMemTable == null) {
      throw new IllegalStateException();
    }

    if (deletingMemTable != null) {
      throw new IllegalStateException();
    }

    if (commitSession == null) {
      throw new IllegalStateException();
    }

    deletingMemTable = otherMemTable;

    otherMemTable = null;
    tablet.notifyAll();
  }

  public void finalizeMinC() {
    if (commitSession == null) {
      throw new IllegalStateException();
    }
    try {
      deletingMemTable.delete(15000);
    } finally {
      synchronized (tablet) {
        if (otherMemTable != null) {
          throw new IllegalStateException();
        }

        if (deletingMemTable == null) {
          throw new IllegalStateException();
        }

        deletingMemTable = null;

        tablet.updateMemoryUsageStats(memTable.estimatedSizeInBytes(), 0);
      }
    }
  }

  public boolean memoryReservedForMinC() {
    return otherMemTable != null || deletingMemTable != null;
  }

  public void waitForMinC() {
    while (otherMemTable != null || deletingMemTable != null) {
      try {
        tablet.wait(50);
      } catch (InterruptedException e) {
        log.warn("{}", e.getMessage(), e);
      }
    }
  }

  public void mutate(CommitSession cm, List mutations, int count) {
    cm.mutate(mutations, count);
  }

  public void updateMemoryUsageStats() {
    long other = 0;
    if (otherMemTable != null)
      other = otherMemTable.estimatedSizeInBytes();
    else if (deletingMemTable != null)
      other = deletingMemTable.estimatedSizeInBytes();

    tablet.updateMemoryUsageStats(memTable.estimatedSizeInBytes(), other);
  }

  public List getIterators(SamplerConfigurationImpl samplerConfig) {
    List toReturn = new ArrayList<>(2);
    toReturn.add(memTable.skvIterator(samplerConfig));
    if (otherMemTable != null)
      toReturn.add(otherMemTable.skvIterator(samplerConfig));
    return toReturn;
  }

  public void returnIterators(List iters) {
    for (MemoryIterator iter : iters) {
      iter.close();
    }
  }

  public long getNumEntries() {
    if (otherMemTable != null)
      return memTable.getNumEntries() + otherMemTable.getNumEntries();
    return memTable.getNumEntries();
  }

  public CommitSession getCommitSession() {
    return commitSession;
  }

  @Override
  public void close() {
    commitSession = null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy