# 写入临时文件

{% hint style="warning" %}
实验性功能
{% endhint %}

在某些情况下，UFS 的性能和带宽可能不能满足大量数据写入的需求。为了解决这个问题，Alluxio 提供了 CACHE\_ONLY 的写入方式： Alluxio 支持将数据只写入到 Alluxio 集群，由于整个过程不与 UFS 交互，写入的性能完全取决于 Alluxio 的性能，写入的带宽完全取决于 Alluxio 节点的带宽， 因此能够在大部分情况下能够获得比写 UFS 更好的性能以及更高的带宽。

推荐的使用场景如下：

* AI 训练过程中临时保存 checkpoint 文件；
* 大数据计算中产生的 shuffle 文件。

在这些场景中，文件仅仅只是临时写入，并不需要长期存储。

## 启用 CACHE\_ONLY

要使用 CACHE\_ONLY 功能，我们必须额外部署 CACHE\_ONLY Storage 组件， CACHE\_ONLY Storage 的数据和元数据由 CACHE\_ONLY Storage 自己管理，不再由 Alluxio Worker 管理。 换句话说，Alluxio 客户端在读取 CACHE\_ONLY Storage 上的文件时，将绕过 Alluxio Worker，直接访问 CACHE\_ONLY Storage。

<figure><img src="/files/blIhYzyPBewju4S8puPv" alt=""><figcaption></figcaption></figure>

### 在 Kubernetes 上部署 CACHE\_ONLY Storage

CACHE\_ONLY Storage 的部署已经集成到 Alluxio operator 中，我们只需要在 Alluxio 的部署文件中启用 CACHE\_ONLY 功能即可。

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: AlluxioCluster
metadata:
  name: alluxio
spec:
  image: <PRIVATE_REGISTRY>/alluxio-enterprise
  imageTag: <TAG>
  properties:

  worker:
    count: 2

  pagestore:
    size: 100Gi

  cacheOnly:
    enabled: true
    mountPath: "/cache-only"
    image: <PRIVATE_REGISTRY>/alluxio-cacheonly
    imageTag: <TAG>
    imagePullPolicy: IfNotPresent

    # Replace with base64 encoded license generated by
    # cat /path/to/license.json | base64 |  tr -d "\n"
    license:

    properties:

    journal:
      storageClass: "gp2"

    worker:
      count: 2
    tieredstore:
      levels:
        - level: 0
          alias: SSD
          mediumtype: SSD
          path: /data1/cacheonly/worker
          type: hostPath
          quota: 10Gi
```

**注意：CACHE\_ONLY Worker 需要本地磁盘存储 CACHE\_ONLY 类型的数据，这部分的磁盘空间与 Alluxio Worker 的缓存完全独立，所以需要提前预估 CACHE\_ONLY 的使用容量，预留磁盘空间。**

### 配置资源使用量

类似 coordinator 和 worker，我们可以使用类似的字段来配置 CACHE\_ONLY Master 和 Worker 使用的资源 `cacheOnly.master.resources` 和 `cacheOnly.worker.resources`。

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: AlluxioCluster
metadata:
  name: alluxio
spec:
  cacheOnly:
    enabled: true
    master:
      count: 1
      resources:
        limits:
          cpu: "8"
          memory: "40Gi"
        requests:
          cpu: "8"
          memory: "40Gi"
      jvmOptions:
        - "-Xmx24g"
        - "-Xms24g"
        - "-XX:MaxDirectMemorySize=8g"
    worker:
      count: 2
      resources:
        limits:
          cpu: "8"
          memory: "20Gi"
        requests:
          cpu: "8"
          memory: "20Gi"
      jvmOptions:
        - "-Xmx8g"
        - "-Xms8g"
        - "-XX:MaxDirectMemorySize=8g"
```

推荐的内存计算方式为：

```
(${Xmx} + ${MaxDirectMemorySize}) * 1.1 <= ${requests} = ${limit}
```

### 访问 CACHE\_ONLY

在 CACHE\_ONLY Storage 部署完成后，所有访问 `/cache_only` 挂载点的请求都是 CACHE\_ONLY 类型的请求，我们可以以多种形式来访问 CACHE\_ONLY 的数据。

使用 Alluxio Cli 访问：

```shell
bin/alluxio fs ls /cache_only
```

使用 Alluxio FUSE 接口访问：

```shell
cd ${fuse_mount_path}/cache_only
echo '123' > test.txt
cat test.txt
```

## 高级配置

### 启用多副本

CACHE\_ONLY 支持写入多副本，需要在部署文件里增加配置 `alluxio.gemini.user.file.replication`：

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: AlluxioCluster
metadata:
  name: alluxio
spec:
  properties:
    "alluxio.gemini.user.file.replication": "2"
```

### 启用 multipart-upload

Alluxio 支持将写入的数据暂存到内存，在后台使用 multipart-upload 多线程上传到 CACHE\_ONLY 集群，这能够有效提高写入的性能。要开启此功能，需要增加以下配置：

```yaml
apiVersion: k8s-operator.alluxio.com/v1
kind: AlluxioCluster
metadata:
  name: alluxio
spec:
  properties:
    "alluxio.gemini.user.file.cache.only.multipart.upload.enabled": "true"
    "alluxio.gemini.user.file.cache.only.multipart.upload.threads": "16"
    "alluxio.gemini.user.file.cache.only.multipart.upload.buffer.number": "16"
```

| 配置项                                                                | 默认值   | 描述                                 |
| ------------------------------------------------------------------ | ----- | ---------------------------------- |
| alluxio.gemini.user.file.cache.only.multipart.upload.enabled       | false | 是否启用 multipart upload 功能           |
| alluxio.gemini.user.file.cache.only.multipart.upload.threads       | 16    | multipart upload 可使用的最大线程数         |
| alluxio.gemini.user.file.cache.only.multipart.upload.buffer.number | 16    | multipart upload 可使用的内存 buffer 的数量 |

**注意：开启 multipart-upload 会显著增加 Alluxio Client 的内存使用量**，内存使用量计算如下：

```
${alluxio.gemini.user.file.cache.only.multipart.upload.buffer.number} * 64MB 
```

### 缓存驱逐

以 CACHE\_ONLY 模式存储的文件不会被自动驱逐。 如果存储容量接近满载，可以手动删除这些文件以释放空间。 比如在 Alluxio FUSE 里使用 `rm ${file_path}` 或者在命令行里使用 `bin/alluxio fs rm ${file_path}`。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://documentation.alluxio.io/ee-ai-cn/ai-3.5/performance/writing-temp-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
