Repository: influxdata/influxdb
File: tsdb/field_validator.go (Go)

Code before:
//   - the point has fields that are too long
func ValidateFields(mf *MeasurementFields, point models.Point, skipSizeValidation bool) error {
	pointSize := point.StringSize()
	iter := point.FieldIterator()
	for iter.Next() {
		if !skipSizeValidation {
			// Check for size of field too large. Note it is much cheaper to check the whole point size
			// than checking the StringValue size (StringValue potentially takes an allocation if it must
			// unescape the string, and must at least parse the string)
			if pointSize > MaxFieldValueLength && iter.Type() == models.String {
				if sz := len(iter.StringValue()); sz > MaxFieldValueLength {
					return PartialWriteError{
						Reason: fmt.Sprintf(
							"input field \"%s\" on measurement \"%s\" is too long, %d > %d",
							iter.FieldKey(), point.Name(), sz, MaxFieldValueLength),
						Dropped: 1,
					}
				}
			}
		}

		// Skip fields name "time", they are illegal.
		if bytes.Equal(iter.FieldKey(), timeBytes) {
			continue
		}

		// If the fields is not present, there cannot be a conflict.
		f := mf.FieldBytes(iter.FieldKey())
		if f == nil {
			continue
		}

		dataType := dataTypeFromModelsFieldType(iter.Type())
		if dataType == influxql.Unknown {
			continue
		}

		// If the types are not the same, there is a conflict.
		if f.Type != dataType {
			return PartialWriteError{
				Reason: fmt.Sprintf(
					"%s: input field \"%s\" on measurement \"%s\" is type %s, already exists as type %s",
					ErrFieldTypeConflict, iter.FieldKey(), point.Name(), dataType, f.Type),
				Dropped: 1,
			}
		}
	}

	return nil
}


Review comment (bug):
My understanding is this is the first part of the data race, where to concurrent writes would both return `nil` here, resulting in this continuing, as "there cannot be a conflict" according to the comment.

Code after:
//   - the point has fields that are too long
func ValidateFields(mf *MeasurementFields, point models.Point, skipSizeValidation bool) ([]*FieldCreate, error) {
	pointSize := point.StringSize()
	iter := point.FieldIterator()
	var fieldsToCreate []*FieldCreate

	for iter.Next() {
		if !skipSizeValidation {
			// Check for size of field too large. Note it is much cheaper to check the whole point size
			// than checking the StringValue size (StringValue potentially takes an allocation if it must
			// unescape the string, and must at least parse the string)
			if pointSize > MaxFieldValueLength && iter.Type() == models.String {
				if sz := len(iter.StringValue()); sz > MaxFieldValueLength {
					return nil, PartialWriteError{
						Reason: fmt.Sprintf(
							"input field \"%s\" on measurement \"%s\" is too long, %d > %d",
							iter.FieldKey(), point.Name(), sz, MaxFieldValueLength),
						Dropped: 1,
					}
				}
			}
		}

		fieldKey := iter.FieldKey()
		// Skip fields name "time", they are illegal.
		if bytes.Equal(fieldKey, timeBytes) {
			continue
		}

		dataType := dataTypeFromModelsFieldType(iter.Type())
		if dataType == influxql.Unknown {
			continue
		}

		// If the field is not present, remember to create it.
		f := mf.FieldBytes(fieldKey)
		if f == nil {
			fieldsToCreate = append(fieldsToCreate, &FieldCreate{
				Measurement: point.Name(),
				Field: &Field{
					Name: string(fieldKey),
					Type: dataType,
				}})
		} else if f.Type != dataType {
			// If the types are not the same, there is a conflict.
			return nil, PartialWriteError{
				Reason: fmt.Sprintf(
					"%s: input field \"%s\" on measurement \"%s\" is type %s, already exists as type %s",
					ErrFieldTypeConflict, fieldKey, point.Name(), dataType, f.Type),
				Dropped: 1,
			}