### 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/test/java/org/elasticsearch/index/store/ByteSizeCachingDirectoryTests.java
public class ByteSizeCachingDirectoryTests extends ESTestCase {
    private static class LengthCountingDirectory extends FilterDirectory {
        int numFileLengthCalls;
        LengthCountingDirectory(Directory in) {
            super(in);
        }
        @Override
        public long fileLength(String name) throws IOException {
            numFileLengthCalls++;
            return super.fileLength(name);
        }
    }
    public void testBasics() throws IOException {
        try (Directory dir = newDirectory()) {
            try (IndexOutput out = dir.createOutput("quux", IOContext.DEFAULT)) {
                out.writeBytes(new byte[11], 11);
            }
            LengthCountingDirectory countingDir = new LengthCountingDirectory(dir);
            ByteSizeCachingDirectory cachingDir = new ByteSizeCachingDirectory(countingDir, new TimeValue(0));
            assertEquals(11, cachingDir.estimateSizeInBytes());
            assertEquals(11, cachingDir.estimateSizeInBytes());
            assertEquals(1, countingDir.numFileLengthCalls);
            try (IndexOutput out = cachingDir.createOutput("foo", IOContext.DEFAULT)) {
                out.writeBytes(new byte[5], 5);
                cachingDir.estimateSizeInBytes();
                
                assertEquals(3, countingDir.numFileLengthCalls);