void setActiveSegment(LogSegmentName name) throws StoreException {
    if (!segmentsByName.containsKey(name)) {
      throw new IllegalArgumentException("There is no log segment with name: " + name);
    }
    ConcurrentNavigableMap<LogSegmentName, LogSegment> extraSegments = segmentsByName.tailMap(name, false);
    Iterator<Map.Entry<LogSegmentName, LogSegment>> iterator = extraSegments.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<LogSegmentName, LogSegment> entry = iterator.next();
      logger.info("Freeing extra segment with name [{}] ", entry.getValue().getName());
      free(entry.getValue(), false);
      remainingUnallocatedSegments.getAndIncrement();
      iterator.remove();
    }
    logger.info("Setting active segment to [{}]", name);
    LogSegment newActiveSegment = segmentsByName.get(name);
    if (newActiveSegment != activeSegment) {
      // If activeSegment needs to be changed, then drop buffer for old activeSegment and init buffer for new activeSegment.
      activeSegment.dropBufferForAppend();
      activeSegment = newActiveSegment;
      activeSegment.initBufferForAppend();
    }
  }
Sets the active segment in the log.
</p>
Frees all segments that follow the active segment. Therefore, this should be
used only after the active segment is conclusively determined.
@param name the name of the log segment that is to be marked active.
@throws IllegalArgumentException if there no segment with name {@code name}.
@throws StoreException if there is any store exception while freeing segments.
ambry-store/src/main/java/com/github/ambry/store/Log.java