Skip to content

Commit

Permalink
jarek/7562: fix heap size fp problem (#7564)
Browse files Browse the repository at this point in the history
* #7562: fix heap size fp problem

* #7562: fix version and heap size fp problem

* #7562 fix jvm settings overwriting whole beakerx config file

* #7562 fix error on beakerx.json not exist

* cleanup formatting of heap size as string

* clean up floating point handling of heap size
  • Loading branch information
jaroslawmalekcodete authored and scottdraves committed Jun 21, 2018
1 parent 34d39e0 commit 6bcfd14
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 21 deletions.
16 changes: 13 additions & 3 deletions beakerx/beakerx/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ def __init__(self):
@staticmethod
def save_setting_to_file(content):
makedirs(paths.jupyter_config_dir(), exist_ok=True)
with fdopen(osopen(EnvironmentSettings.config_path, O_RDWR | O_CREAT | O_TRUNC, 0o600), 'w+') as file:
file.write(json.dumps(json.loads(content), indent=4, sort_keys=True))
with fdopen(osopen(EnvironmentSettings.config_path, O_RDWR | O_CREAT, 0o600), 'w+') as file:
file_content = file.read()
new_settings = json.loads(content)
if file_content:
saved_settings = json.loads(file_content)
file.seek(0)
file.truncate()
for setting_name in new_settings['beakerx']:
saved_settings['beakerx'][setting_name] = new_settings['beakerx'][setting_name]
else:
saved_settings = new_settings
file.write(json.dumps(saved_settings, indent=4, sort_keys=True))

@staticmethod
def read_setting_from_file():
Expand Down Expand Up @@ -108,7 +118,7 @@ def read_beakerx_env_settings():
if 'heap_GB' in jvm_settings and jvm_settings['heap_GB']:
val = float(jvm_settings['heap_GB'])
if val.is_integer():
value = '-Xmx' + str(jvm_settings['heap_GB']) + 'g'
value = '-Xmx' + str(int(val)) + 'g'
else:
value = '-Xmx' + str(int(val * 1024)) + 'm'
args.append(value)
Expand Down
6 changes: 3 additions & 3 deletions js/notebook/src/tree/Models/DefaultOptionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export default class DefaultOptionsModel {
}

public static normaliseHeapSize(heap_GB: number): string {
if (heap_GB % 1 === 0) {
return `${heap_GB}g`;
if (Number.isInteger(heap_GB)) {
return heap_GB + 'g';
}
return parseInt(`${heap_GB * 1024}`) + 'm';
return Math.floor(heap_GB * 1024) + 'm';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void loadProfile() {
this.executorCores.setValue(profileData.getOrDefault(SparkUI.SPARK_EXECUTOR_CORES, SparkUI.SPARK_EXECUTOR_CORES_DEFAULT));
this.executorMemory.setValue(profileData.getOrDefault(SparkUI.SPARK_EXECUTOR_MEMORY, SparkUI.SPARK_EXECUTOR_MEMORY_DEFAULT));
Map<String, String> advancedSettings = new HashMap<>();
((ArrayList<LinkedTreeMap>)profileData.getOrDefault(SparkUI.SPARK_ADVANCED_OPTIONS, SparkUI.SPARK_ADVANCED_OPTIONS_DEFAULT))
((ArrayList<Map>)profileData.getOrDefault(SparkUI.SPARK_ADVANCED_OPTIONS, SparkUI.SPARK_ADVANCED_OPTIONS_DEFAULT))
.stream()
.forEach(x -> {
advancedSettings.put(x.get("name").toString(), x.get("value").toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package com.twosigma.beakerx.widget;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;
import scala.Tuple2;
Expand All @@ -29,15 +28,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static com.twosigma.beakerx.widget.SparkUI.BEAKERX_ID;
import static com.twosigma.beakerx.widget.SparkUI.SPARK_ADVANCED_OPTIONS_DEFAULT;
import static com.twosigma.beakerx.widget.SparkUI.SPARK_APP_NAME;
import static com.twosigma.beakerx.widget.SparkUI.SPARK_EXECUTOR_CORES_DEFAULT;
import static com.twosigma.beakerx.widget.SparkUI.SPARK_EXECUTOR_MEMORY_DEFAULT;
Expand All @@ -59,12 +56,14 @@ public class SparkUiDefaultsImpl implements SparkUiDefaults {
private static final String CURRENT_PROFILE = "current_profile";

private List<Map<String, Object>> profiles = new ArrayList<>();
private Gson gson = new GsonBuilder().setPrettyPrinting().create();
private ObjectMapper objectMapper;
private Path path;
private String currentProfile = DEFAULT_PROFILE;

public SparkUiDefaultsImpl(Path path) {
this.path = path;
this.objectMapper = new ObjectMapper();
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}

public void saveSparkConf(List<Map<String, Object>> profiles) {
Expand All @@ -73,7 +72,7 @@ public void saveSparkConf(List<Map<String, Object>> profiles) {
Map<String, Object> sparkOptions = (Map<String, Object>) map.get(BEAKERX).getOrDefault(SPARK_OPTIONS, new HashMap<>());
sparkOptions.put(SPARK_PROFILES, profiles == null ? new ArrayList<>() : profiles);
map.get(BEAKERX).put(SPARK_OPTIONS, sparkOptions);
String content = gson.toJson(map);
String content = toJson(map);
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
this.profiles = profiles;
} catch (IOException e) {
Expand Down Expand Up @@ -145,11 +144,12 @@ public List<String> getProfileNames() {
@Override
public void saveProfileName(String profileName) {
try {

Map<String, Map> map = beakerxJsonAsMap(path);
Map<String, Object> sparkOptions = (Map<String, Object>) map.get(BEAKERX).getOrDefault(SPARK_OPTIONS, new HashMap<>());
sparkOptions.put(CURRENT_PROFILE, profileName);
map.get(BEAKERX).put(SPARK_OPTIONS, sparkOptions);
String content = gson.toJson(map);
String content = toJson(map);
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
currentProfile = profileName;
} catch (IOException e) {
Expand Down Expand Up @@ -195,7 +195,7 @@ Map<String, Map> beakerxJsonAsMap(Path path) {
} catch (Exception e) {
throw new RuntimeException(e);
}
return (Map<String, Map>) gson.fromJson(jsonAsString, Map.class);
return fromJson(jsonAsString, LinkedHashMap.class);
}

private Map<String, Object> toMap(SparkConf sparkConf) {
Expand Down Expand Up @@ -231,4 +231,23 @@ private List<Map<String, String>> getProps(Map<String, Object> result) {
return (List<Map<String, String>>) propsAsObject;
}

private <T> T fromJson(String json, Class<T> theClass) {
T result = null;
try {
result = objectMapper.readValue(json, theClass);
} catch (Exception e) {
// Ignored.
}

return result;
}

private String toJson(Object object) {
try {
return objectMapper.writeValueAsString(object);
} catch (Exception e) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import org.apache.spark.SparkConf;
import org.apache.spark.sql.SparkSession;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand All @@ -36,7 +38,6 @@
import static com.twosigma.beakerx.widget.SparkUiDefaults.DEFAULT_PROFILE;
import static com.twosigma.beakerx.widget.SparkUiDefaultsImpl.BEAKERX;
import static com.twosigma.beakerx.widget.SparkUiDefaultsImpl.PROPERTIES;
import static com.twosigma.beakerx.widget.SparkUiDefaultsImpl.SPARK_OPTIONS;
import static com.twosigma.beakerx.widget.SparkUiDefaultsImpl.VALUE;
import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -56,6 +57,20 @@ public void setUp() {
this.sut = new SparkUiDefaultsImpl(pathToBeakerxTestJson);
}

@Test
public void loadAndSaveWithoutChangesShouldBeIdempotent() throws IOException {
//given
SparkSession.Builder builder = SparkSession.builder();
HashMap<String, Object> profileConfig = new HashMap<>();
profileConfig.put(NAME, DEFAULT_PROFILE);
//when
sut.loadDefaults(builder);
sut.saveProfile(profileConfig);
//then
Map<String, Map> result = sut.beakerxJsonAsMap(pathToBeakerxTestJson);
Assertions.assertThat(result.get("beakerx").get("version")).isEqualTo(2);
}

@Test
public void saveMasterURL() {
//given
Expand Down Expand Up @@ -137,7 +152,7 @@ public void saveAndLoadDefaults() {
//given
HashMap<String, Object> profileConfig = new HashMap<>();
profileConfig.put(SPARK_ADVANCED_OPTIONS, Arrays.asList(
new SparkConfiguration.Configuration("sparkOption2", "sp2")));
new SparkConfiguration.Configuration("sparkOption2", "3")));
profileConfig.put(SPARK_MASTER, "local[4]");
profileConfig.put(NAME, DEFAULT_PROFILE);

Expand All @@ -149,7 +164,7 @@ public void saveAndLoadDefaults() {
SparkSession.Builder builder = SparkSession.builder();
sut.loadDefaults(builder);
SparkConf sparkConfBasedOn = SparkEngineImpl.getSparkConfBasedOn(builder);
assertThat(sparkConfBasedOn.get("sparkOption2")).isEqualTo("sp2");
assertThat(sparkConfBasedOn.get("sparkOption2")).isEqualTo("3");
assertThat(sparkConfBasedOn.get(SPARK_MASTER)).isEqualTo("local[4]");
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/sparkex/src/test/resources/beakerxTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"ui_options": {
"auto_close": true
},
"version": 2.0
"version": 2
}
}

0 comments on commit 6bcfd14

Please sign in to comment.