Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins: Add status bar on download (#18695)
As some plugins are becoming big now, it is hard for the user to know, if the plugin is being downloaded or just nothing happens. This commit adds a progress bar during download, which can be disabled by using the `-q` parameter. In addition this updates to jimfs 1.1, which allows us to test the batch mode, as adding security policies are now supported due to having jimfs:// protocol support in URL stream handlers.
- Loading branch information
Showing
8 changed files
with
315 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
core/src/main/java/org/elasticsearch/plugins/ProgressInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.plugins; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* An input stream that allows to add a listener to monitor progress | ||
* The listener is triggered whenever a full percent is increased | ||
* The listener is never triggered twice on the same percentage | ||
* The listener will always return 99 percent, if the expectedTotalSize is exceeded, until it is finished | ||
* | ||
* Only used by the InstallPluginCommand, thus package private here | ||
*/ | ||
abstract class ProgressInputStream extends FilterInputStream { | ||
|
||
private final int expectedTotalSize; | ||
private int currentPercent; | ||
private int count = 0; | ||
|
||
public ProgressInputStream(InputStream is, int expectedTotalSize) { | ||
super(is); | ||
this.expectedTotalSize = expectedTotalSize; | ||
this.currentPercent = 0; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int read = in.read(); | ||
checkProgress(read == -1 ? -1 : 1); | ||
return read; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int byteCount = super.read(b, off, len); | ||
checkProgress(byteCount); | ||
return byteCount; | ||
} | ||
|
||
@Override | ||
public int read(byte b[]) throws IOException { | ||
return read(b, 0, b.length); | ||
} | ||
|
||
void checkProgress(int byteCount) { | ||
// are we done? | ||
if (byteCount == -1) { | ||
currentPercent = 100; | ||
onProgress(currentPercent); | ||
} else { | ||
count += byteCount; | ||
// rounding up to 100% would mean we say we are done, before we are... | ||
// this also catches issues, when expectedTotalSize was guessed wrong | ||
int percent = Math.min(99, (int) Math.floor(100.0*count/expectedTotalSize)); | ||
if (percent > currentPercent) { | ||
currentPercent = percent; | ||
onProgress(percent); | ||
} | ||
} | ||
} | ||
|
||
public void onProgress(int percent) {} | ||
} |
116 changes: 116 additions & 0 deletions
116
core/src/test/java/org/elasticsearch/plugins/ProgressInputStreamTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.plugins; | ||
|
||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.hasItems; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
public class ProgressInputStreamTests extends ESTestCase { | ||
|
||
private List<Integer> progresses = new ArrayList<>(); | ||
|
||
public void testThatProgressListenerIsCalled() throws Exception { | ||
ProgressInputStream is = newProgressInputStream(0); | ||
is.checkProgress(-1); | ||
|
||
assertThat(progresses, hasSize(1)); | ||
assertThat(progresses, hasItems(100)); | ||
} | ||
|
||
public void testThatProgressListenerIsCalledOnUnexpectedCompletion() throws Exception { | ||
ProgressInputStream is = newProgressInputStream(2); | ||
is.checkProgress(-1); | ||
assertThat(progresses, hasItems(100)); | ||
} | ||
|
||
public void testThatProgressListenerReturnsMaxValueOnWrongExpectedSize() throws Exception { | ||
ProgressInputStream is = newProgressInputStream(2); | ||
|
||
is.checkProgress(1); | ||
assertThat(progresses, hasItems(50)); | ||
|
||
is.checkProgress(3); | ||
assertThat(progresses, hasItems(50, 99)); | ||
|
||
is.checkProgress(-1); | ||
assertThat(progresses, hasItems(50, 99, 100)); | ||
} | ||
|
||
public void testOneByte() throws Exception { | ||
ProgressInputStream is = newProgressInputStream(1); | ||
is.checkProgress(1); | ||
is.checkProgress(-1); | ||
|
||
assertThat(progresses, hasItems(99, 100)); | ||
|
||
} | ||
|
||
public void testOddBytes() throws Exception { | ||
int odd = (randomIntBetween(100, 200) / 2) + 1; | ||
ProgressInputStream is = newProgressInputStream(odd); | ||
for (int i = 0; i < odd; i++) { | ||
is.checkProgress(1); | ||
} | ||
is.checkProgress(-1); | ||
|
||
assertThat(progresses, hasSize(odd+1)); | ||
assertThat(progresses, hasItem(100)); | ||
} | ||
|
||
public void testEvenBytes() throws Exception { | ||
int even = (randomIntBetween(100, 200) / 2); | ||
ProgressInputStream is = newProgressInputStream(even); | ||
|
||
for (int i = 0; i < even; i++) { | ||
is.checkProgress(1); | ||
} | ||
is.checkProgress(-1); | ||
|
||
assertThat(progresses, hasSize(even+1)); | ||
assertThat(progresses, hasItem(100)); | ||
} | ||
|
||
public void testOnProgressCannotBeCalledMoreThanOncePerPercent() throws Exception { | ||
int count = randomIntBetween(150, 300); | ||
ProgressInputStream is = newProgressInputStream(count); | ||
|
||
for (int i = 0; i < count; i++) { | ||
is.checkProgress(1); | ||
} | ||
is.checkProgress(-1); | ||
|
||
assertThat(progresses, hasSize(100)); | ||
} | ||
|
||
private ProgressInputStream newProgressInputStream(int expectedSize) { | ||
return new ProgressInputStream(null, expectedSize) { | ||
@Override | ||
public void onProgress(int percent) { | ||
progresses.add(percent); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.