### Elasticsearch Version

7.10.2

### Installed Plugins

repository-hdfs

### Java Version

_bundled_

### OS Version

Linux 4.14.81.bm.26-amd64 #1 SMP Debian 4.14.81.bm.26 Mon Sep 14 09:46:45 UTC 2020

### Problem Description

The size of shard should't  be computed again instead of using cached when no pending write。we migrated a lot of indicess to a new cluster by snapshot, nodes and indices stats api become very slow because some shards that were restored from snapshots always computed shard directory size even no write，not use cached because the numOpenOutputs was decreased to a negative number

### Steps to Reproduce

snapshot a index then restore, the shard that recovery from snapshot will always compute directory size instead of using cached

### Logs (if relevant)

![image](https://user-images.githubusercontent.com/17610965/208378334-c814a7ab-c78e-439d-8444-99a511868604.png)



server/src/main/java/org/elasticsearch/index/store/ByteSizeCachingDirectory.java
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */
package org.elasticsearch.index.store;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.elasticsearch.common.lucene.store.FilterIndexOutput;
import org.elasticsearch.common.util.SingleObjectCache;
import org.elasticsearch.core.TimeValue;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.NoSuchFileException;
final class ByteSizeCachingDirectory extends FilterDirectory {