/** @throws Exception If failed. */
  public void testCreateFileFragmented() throws Exception {
    GridGgfsEx impl = (GridGgfsEx) grid(0).ggfs("ggfs");

    GridGgfsFragmentizerManager fragmentizer = impl.context().fragmentizer();

    GridTestUtils.setFieldValue(fragmentizer, "fragmentizerEnabled", false);

    GridGgfsPath path = new GridGgfsPath("/file");

    try {
      GridGgfs fs0 = grid(0).ggfs("ggfs");
      GridGgfs fs1 = grid(1).ggfs("ggfs");
      GridGgfs fs2 = grid(2).ggfs("ggfs");

      try (GridGgfsOutputStream out =
          fs0.create(
              path,
              128,
              false,
              1,
              CFG_GRP_SIZE,
              F.asMap(GridGgfs.PROP_PREFER_LOCAL_WRITES, "true"))) {
        // 1.5 blocks
        byte[] data = new byte[CFG_BLOCK_SIZE * 3 / 2];

        Arrays.fill(data, (byte) 1);

        out.write(data);
      }

      try (GridGgfsOutputStream out = fs1.append(path, false)) {
        // 1.5 blocks.
        byte[] data = new byte[CFG_BLOCK_SIZE * 3 / 2];

        Arrays.fill(data, (byte) 2);

        out.write(data);
      }

      // After this we should have first two block colocated with grid 0 and last block colocated
      // with grid 1.
      GridGgfsFileImpl fileImpl = (GridGgfsFileImpl) fs.info(path);

      GridCache<Object, Object> metaCache = grid(0).cachex(META_CACHE_NAME);

      GridGgfsFileInfo fileInfo = (GridGgfsFileInfo) metaCache.get(fileImpl.fileId());

      GridGgfsFileMap map = fileInfo.fileMap();

      List<GridGgfsFileAffinityRange> ranges = map.ranges();

      assertEquals(2, ranges.size());

      assertTrue(ranges.get(0).startOffset() == 0);
      assertTrue(ranges.get(0).endOffset() == 2 * CFG_BLOCK_SIZE - 1);

      assertTrue(ranges.get(1).startOffset() == 2 * CFG_BLOCK_SIZE);
      assertTrue(ranges.get(1).endOffset() == 3 * CFG_BLOCK_SIZE - 1);

      // Validate data read after colocated writes.
      try (GridGgfsInputStream in = fs2.open(path)) {
        // Validate first part of file.
        for (int i = 0; i < CFG_BLOCK_SIZE * 3 / 2; i++) assertEquals((byte) 1, in.read());

        // Validate second part of file.
        for (int i = 0; i < CFG_BLOCK_SIZE * 3 / 2; i++) assertEquals((byte) 2, in.read());

        assertEquals(-1, in.read());
      }
    } finally {
      GridTestUtils.setFieldValue(fragmentizer, "fragmentizerEnabled", true);

      boolean hasData = false;

      for (int i = 0; i < NODES_CNT; i++) hasData |= !grid(i).cachex(DATA_CACHE_NAME).isEmpty();

      assertTrue(hasData);

      fs.delete(path, true);
    }

    GridTestUtils.retryAssert(
        log,
        ASSERT_RETRIES,
        ASSERT_RETRY_INTERVAL,
        new CAX() {
          @Override
          public void applyx() {
            for (int i = 0; i < NODES_CNT; i++)
              assertTrue(grid(i).cachex(DATA_CACHE_NAME).isEmpty());
          }
        });
  }
  /**
   * @param seq Start/stop sequence.
   * @throws Exception If failed.
   */
  private void checkSequence0(boolean[] seq) throws Exception {
    try {
      startGrid(0);

      TreeSet<Integer> started = new TreeSet<>();

      started.add(0);

      int topVer = 1;

      for (boolean start : seq) {
        if (start) {
          int nextIdx = nextIndex(started);

          startGrid(nextIdx);

          started.add(nextIdx);
        } else {
          int idx = started.last();

          stopGrid(idx);

          started.remove(idx);
        }

        topVer++;

        info("Grid 0: " + grid(0).localNode().id());

        ((GridKernal) grid(0))
            .internalCache()
            .context()
            .affinity()
            .affinityReadyFuture(topVer)
            .get();

        for (int i : started) {
          if (i != 0) {
            GridEx grid = grid(i);

            ((GridKernal) grid)
                .internalCache()
                .context()
                .affinity()
                .affinityReadyFuture(topVer)
                .get();

            info("Grid " + i + ": " + grid.localNode().id());

            for (int part = 0; part < parts; part++) {
              List<GridNode> firstNodes =
                  (List<GridNode>)
                      grid(0).cache(null).affinity().mapPartitionToPrimaryAndBackups(part);

              List<GridNode> secondNodes =
                  (List<GridNode>)
                      grid.cache(null).affinity().mapPartitionToPrimaryAndBackups(part);

              assertEquals(firstNodes.size(), secondNodes.size());

              for (int n = 0; n < firstNodes.size(); n++)
                assertEquals(firstNodes.get(n), secondNodes.get(n));
            }
          }
        }
      }
    } finally {
      stopAllGrids();
    }
  }