Skip to content

Add insertTablets(List<Tablet>) and insertAlignedTablets(List<Tablet>) to Session interface - #19093

Open
yamass wants to merge 1 commit into
apache:masterfrom
yamass:feature/session-insert-tablets-list
Open

yamass wants to merge 1 commit into
apache:masterfrom
yamass:feature/session-insert-tablets-list

Conversation

@yamass

@yamass yamass commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Description

Introduce new Session methods: insertTablets(List<Tablet>) and insertAlignedTablets(List<Tablet>).

The existing methods insertTablets(Map<String, Tablet>) and insertAlignedTablets(Map<String, Tablet>) allow only one tablet per device, because the map is keyed by device id.

This PR adds insertTablets(List<Tablet>) and insertAlignedTablets(List<Tablet>) to ISession/Session and ISessionPool/SessionPool. They accept several tablets of the same device and send them in one request. With redirection enabled, each tablet is routed by its own device id, so tablets of devices with the same leader go to that leader together. No behaviour change for existing methods.

Why

The devices in our system sample their measurements independently, so the timestamps of their measurements rarely match. Packing such measurements into one tablet per device fills most value cells with nulls. So in our case, the natural representation is one tablet per measurement, i.e. several tablets per device. TSInsertTabletsReq already accepts the same device several times; only the client API does not.

Workarounds

With the current map-based API, there are two workarounds to send several tablets of one device in one request:

  1. Key the tablets by something unique, e.g. the timeseries path. This works, but the map key is what selects the connection, and such keys are never in the leader cache. So with redirection enabled, every request goes to the default connection, and the receiving DataNode has to forward it to the leader.
  2. Pass a fake Map that contains the same device id several times, one entry per tablet. This routes correctly, but breaks the Map contract and relies on Session only iterating the map. If Session ever copied it into a real map, all but one tablet per device would be dropped without an error. This is our current workaround, guarded by tests that fail if an IoTDB upgrade breaks it.

Changes

  • Session.insertTablets(List) / insertAlignedTablets(List): with redirection enabled, they key each tablet by its device id and use the existing leader-cache path. Without redirection, they send one request over the default connection, like the map methods.
  • insertTabletsWithLeaderCache now takes a list of KeyedTablet(leaderCacheKey, tablet) instead of a map, so a key may occur several times. The map methods use their map keys as leader cache keys, so their routing stays the same.
  • No sorted overloads: sorted is deprecated, and unsorted tablets are sorted anyway.

Tests

SessionInsertTabletListTest (new) uses a mocked connection that records every request and, like a DataNode, answers with a redirect when a request contains devices led by another endpoint.

Test Checks
test_InsertTablets_WhenLeadersAreUnknown_SendsAllTabletsToDefaultEndpoint first request goes to the default endpoint; the redirect fills the leader cache
test_InsertTablets_WhenLeadersAreKnown_SendsOneRequestPerLeader three tablets of one device go to its leader in one request, another device's tablet to its own leader
test_InsertTablets_WhenRedirectionIsDisabled_SendsOneRequestToDefaultEndpoint one non-aligned request over the default connection
test_InsertAlignedTablets_WhenLeadersAreKnown_SendsAlignedRequestPerLeader aligned flag and routing
test_InsertAlignedTablets_WhenRedirectionIsDisabled_SendsOneAlignedRequest aligned flag without redirection

SessionPoolTest:

Test Checks
test_InsertTabletList_WhenSessionAcceptsIt_PassesTabletsOnAndPutsSessionBack pool delegates and puts the session back
test_InsertAlignedTabletList_WhenSessionAcceptsIt_PassesTabletsOnAndPutsSessionBack same, aligned
test_InsertTabletList_WhenSessionRejectsIt_RethrowsAndPutsSessionBack rejected insert
test_InsertAlignedTabletList_WhenConnectionBreaks_ClosesSessionAndThrows connection error

This PR has:

  • been self-reviewed.
  • added Javadocs for most classes and all non-trivial methods.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold
    for code coverage.

Key changed/added classes (or packages if there are too many classes) in this PR
  • ISession, ISessionPool, Session, SessionPool + tests

…) to Session and SessionPool

The map-based insertTablets methods allow only one tablet per device,
because the map is keyed by device id. The new list overloads accept
several tablets of the same device and send them in one request. With
redirection enabled, each tablet is routed by its own device id, so
tablets of devices with the same leader go to that leader together.

insertTabletsWithLeaderCache now takes a list of KeyedTablet instead of
a map, so a leader cache key may occur several times. The map methods
keep routing by their map keys.
@HTHou
HTHou requested a review from JackieTien97 November 3, 2026 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant