1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
7 import { ErasureCodeProfileService } from '../../../shared/api/erasure-code-profile.service';
8 import { CrushNodeSelectionClass } from '../../../shared/classes/crush.node.selection.class';
9 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
10 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
11 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
12 import { CdValidators } from '../../../shared/forms/cd-validators';
13 import { CrushNode } from '../../../shared/models/crush-node';
14 import { ErasureCodeProfile } from '../../../shared/models/erasure-code-profile';
15 import { FinishedTask } from '../../../shared/models/finished-task';
16 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
19 selector: 'cd-erasure-code-profile-form-modal',
20 templateUrl: './erasure-code-profile-form-modal.component.html',
21 styleUrls: ['./erasure-code-profile-form-modal.component.scss']
23 export class ErasureCodeProfileFormModalComponent extends CrushNodeSelectionClass
26 submitAction = new EventEmitter();
28 tooltips = this.ecpService.formTooltips;
30 LRC: 'lrc', // Locally Repairable Erasure Code
31 SHEC: 'shec', // Shingled Erasure Code
32 JERASURE: 'jerasure', // default
33 ISA: 'isa' // Intel Storage Acceleration
35 plugin = this.PLUGIN.JERASURE;
47 private formBuilder: CdFormBuilder,
48 public bsModalRef: BsModalRef,
49 private taskWrapper: TaskWrapperService,
50 private ecpService: ErasureCodeProfileService,
52 public actionLabels: ActionLabelsI18n
55 this.action = this.actionLabels.CREATE;
56 this.resource = this.i18n('EC Profile');
58 this.setJerasureDefaults();
62 this.form = this.formBuilder.group({
67 Validators.pattern('[A-Za-z0-9_-]+'),
70 (value: string) => this.names && this.names.indexOf(value) !== -1
74 plugin: [this.PLUGIN.JERASURE, [Validators.required]],
76 4, // Will be overwritten with plugin defaults
80 CdValidators.custom('max', () => this.baseValueValidation(true)),
81 CdValidators.custom('unequal', (v: number) => this.lrcDataValidation(v)),
82 CdValidators.custom('kLowerM', (v: number) => this.shecDataValidation(v))
86 2, // Will be overwritten with plugin defaults
90 CdValidators.custom('max', () => this.baseValueValidation())
93 crushFailureDomain: '', // Will be preselected
94 crushRoot: null, // Will be preselected
95 crushDeviceClass: '', // Will be preselected
97 // Only for 'jerasure' and 'isa' use
98 technique: 'reed_sol_van',
99 // Only for 'jerasure' use
100 packetSize: [2048, [Validators.min(1)]],
101 // Only for 'lrc' use
103 3, // Will be overwritten with plugin defaults
107 CdValidators.custom('unequal', (v: number) => this.lrcLocalityValidation(v))
110 crushLocality: '', // set to none at the end (same list as for failure domains)
111 // Only for 'shec' use
113 2, // Will be overwritten with plugin defaults
117 CdValidators.custom('cGreaterM', (v: number) => this.shecDurabilityValidation(v))
121 this.form.get('k').valueChanges.subscribe(() => this.updateValidityOnChange(['m', 'l']));
122 this.form.get('m').valueChanges.subscribe(() => this.updateValidityOnChange(['k', 'l', 'c']));
123 this.form.get('l').valueChanges.subscribe(() => this.updateValidityOnChange(['k', 'm']));
124 this.form.get('plugin').valueChanges.subscribe((plugin) => this.onPluginChange(plugin));
127 private baseValueValidation(dataChunk: boolean = false): boolean {
128 return this.validValidation(() => {
130 this.getKMSum() > this.deviceCount &&
131 this.form.getValue('k') > this.form.getValue('m') === dataChunk
136 private validValidation(fn: () => boolean, plugin?: string): boolean {
137 if (!this.form || plugin ? this.plugin !== plugin : false) {
143 private getKMSum(): number {
144 return this.form.getValue('k') + this.form.getValue('m');
147 private lrcDataValidation(k: number): boolean {
148 return this.validValidation(() => {
149 const m = this.form.getValue('m');
150 const l = this.form.getValue('l');
152 this.lrcMultiK = k / (km / l);
153 return k % (km / l) !== 0;
157 private shecDataValidation(k: number): boolean {
158 return this.validValidation(() => {
159 const m = this.form.getValue('m');
164 private lrcLocalityValidation(l: number) {
165 return this.validValidation(() => {
166 const value = this.getKMSum();
167 this.lrcGroups = l > 0 ? value / l : 0;
168 return l > 0 && value % l !== 0;
172 private shecDurabilityValidation(c: number): boolean {
173 return this.validValidation(() => {
174 const m = this.form.getValue('m');
179 private updateValidityOnChange(names: string[]) {
180 names.forEach((name) => this.form.get(name).updateValueAndValidity({ emitEvent: false }));
183 private onPluginChange(plugin: string) {
184 this.plugin = plugin;
185 if (plugin === this.PLUGIN.JERASURE) {
186 this.setJerasureDefaults();
187 } else if (plugin === this.PLUGIN.LRC) {
188 this.setLrcDefaults();
189 } else if (plugin === this.PLUGIN.ISA) {
190 this.setIsaDefaults();
191 } else if (plugin === this.PLUGIN.SHEC) {
192 this.setShecDefaults();
194 this.updateValidityOnChange(['m']); // Triggers k, m, c and l
197 private setJerasureDefaults() {
213 private setLrcDefaults() {
221 private setIsaDefaults() {
223 * Actually k and m are not required - but they will be set to the default values in case
224 * if they are not set, therefore it's fine to mark them as required in order to get
225 * strange values that weren't set.
231 this.techniques = ['reed_sol_van', 'cauchy'];
234 private setShecDefaults() {
236 * Actually k, c and m are not required - but they will be set to the default values in case
237 * if they are not set, therefore it's fine to mark them as required in order to get
238 * strange values that weren't set.
247 private setDefaults(defaults: object) {
248 Object.keys(defaults).forEach((controlName) => {
249 const control = this.form.get(controlName);
250 const value = control.value;
251 let overwrite = control.pristine;
253 * As k, m, c and l are now set touched and dirty on the beginning, plugin change will
254 * overwrite their values as we can't determine if the user has changed anything.
255 * k and m can have two default values where as l and c can only have one,
256 * so there is no need to overwrite them.
258 if ('k' === controlName) {
259 overwrite = [4, 7].includes(value);
260 } else if ('m' === controlName) {
261 overwrite = [2, 3].includes(value);
264 this.form.get(controlName).setValue(defaults[controlName]);
284 this.initCrushNodeSelection(
286 this.form.get('crushRoot'),
287 this.form.get('crushFailureDomain'),
288 this.form.get('crushDeviceClass')
290 this.plugins = plugins;
292 this.form.silentSet('directory', directory);
293 this.preValidateNumericInputFields();
299 * This allows k, m, l and c to be validated instantly on change, before the
300 * fields got changed before by the user.
302 private preValidateNumericInputFields() {
303 const kml = ['k', 'm', 'l', 'c'].map((name) => this.form.get(name));
304 kml.forEach((control) => {
305 control.markAsTouched();
306 control.markAsDirty();
308 kml[1].updateValueAndValidity(); // Update validity of k, m, c and l
312 if (this.form.invalid) {
313 this.form.setErrors({ cdSubmitButton: true });
316 const profile = this.createJson();
318 .wrapTaskAroundCall({
319 task: new FinishedTask('ecp/create', { name: profile.name }),
320 call: this.ecpService.create(profile)
325 this.form.setErrors({ cdSubmitButton: true });
328 this.bsModalRef.hide();
329 this.submitAction.emit(profile);
334 private createJson() {
335 const pluginControls = {
336 technique: [this.PLUGIN.ISA, this.PLUGIN.JERASURE],
337 packetSize: [this.PLUGIN.JERASURE],
338 l: [this.PLUGIN.LRC],
339 crushLocality: [this.PLUGIN.LRC],
340 c: [this.PLUGIN.SHEC]
342 const ecp = new ErasureCodeProfile();
343 const plugin = this.form.getValue('plugin');
344 Object.keys(this.form.controls)
346 const pluginControl = pluginControls[name];
347 const value = this.form.getValue(name);
348 const usable = (pluginControl && pluginControl.includes(plugin)) || !pluginControl;
349 return usable && value && value !== '';
352 this.extendJson(name, ecp);
357 private extendJson(name: string, ecp: ErasureCodeProfile) {
358 const differentApiAttributes = {
359 crushFailureDomain: 'crush-failure-domain',
360 crushRoot: 'crush-root',
361 crushDeviceClass: 'crush-device-class',
362 packetSize: 'packetsize',
363 crushLocality: 'crush-locality'
365 const value = this.form.getValue(name);
366 ecp[differentApiAttributes[name] || name] = name === 'crushRoot' ? value.name : value;