code: def _create_and_save_state(cls, mapreduce_spec, _app):
      
      state = model.MapreduceState.create_new(mapreduce_spec.mapreduce_id)
      state.mapreduce_spec = mapreduce_spec
      state.active = True
      state.active_shards = 0
      if _app:
        state.app_id = _app
      config = util.create_datastore_write_config(mapreduce_spec)
      state.put(config=config)
      return state
docs: Save mapreduce state to datastore.
  
      Save state to datastore so that UI can see it immediately.
  
      Args:
        mapreduce_spec: model.MapreduceSpec,
        _app: app id if specified. None otherwise.
  
      Returns:
        The saved Mapreduce state.