public synchronized long nextGtid() {
        long timestamp = timeGen();

        if (timestamp < lastTimestamp) {
            timestamp = lastTimestamp;
        }

        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & MAX_SEQUENCE;
            if (sequence == 0) {
                timestamp += 1;
            }
        } else {
            sequence = 0L;
        }

        if (timestamp - EPOCH >= (1L << 42)) {
            throw new IllegalStateException("Timestamp overflow");
        }

        lastTimestamp = timestamp;
        return ((timestamp - EPOCH) << TIMESTAMP_SHIFT) | (CLUSTER_ID << CLUSTER_ID_SHIFT) | sequence;
    }
@Test
    public void testNextGtidIncrementsSequenceOnSameMillisecond() {
        long firstGtid = gtidGenerator.nextGtid();
        long secondGtid = gtidGenerator.nextGtid();

        Assertions.assertNotEquals(firstGtid, secondGtid, "GTIDs should be unique");
        Assertions.assertEquals((firstGtid & GtidGenerator.MAX_SEQUENCE) + 1, secondGtid & GtidGenerator.MAX_SEQUENCE,
                "Sequence should increment by 1 on the same millisecond");
    }