File: prefetch.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (150 lines) | stat: -rw-r--r-- 4,678 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

/**
 * @file prefetch.cpp
 * @brief Background fetch related classes (header file).
 */

#include "ts/ts.h" /* ATS API */
#include "prefetch.h"

bool
BgBlockFetch::schedule(Data *const data, int blocknum)
{
  bool ret         = false;
  BgBlockFetch *bg = new BgBlockFetch(blocknum);
  if (bg->fetch(data)) {
    ret = true;
  } else {
    delete bg;
  }
  return ret;
}

/**
 * Initialize and schedule the background fetch
 */
bool
BgBlockFetch::fetch(Data *const data)
{
  if (m_stream.m_read.isOpen()) {
    // should never happen since the connection was just initialized
    ERROR_LOG("Background block request already in flight!");
    return false;
  }

  int64_t const blockbeg = (data->m_config->m_blockbytes * m_blocknum);
  Range blockbe(blockbeg, blockbeg + data->m_config->m_blockbytes);

  char rangestr[1024];
  int rangelen      = sizeof(rangestr);
  bool const rpstat = blockbe.toStringClosed(rangestr, &rangelen);
  TSAssert(rpstat);

  DEBUG_LOG("Request background block: %s", rangestr);

  // reuse the incoming client header, just change the range
  HttpHeader header(data->m_req_hdrmgr.m_buffer, data->m_req_hdrmgr.m_lochdr);

  // add/set sub range key and add slicer tag
  bool const rangestat = header.setKeyVal(TS_MIME_FIELD_RANGE, TS_MIME_LEN_RANGE, rangestr, rangelen);

  if (!rangestat) {
    ERROR_LOG("Error trying to set range request header %s", rangestr);
    return false;
  }
  TSAssert(nullptr == m_cont);

  // Setup the continuation
  m_cont = TSContCreate(handler, TSMutexCreate());
  TSContDataSet(m_cont, static_cast<void *>(this));

  // create virtual connection back into ATS
  TSHttpConnectOptions options = TSHttpConnectOptionsGet(TS_CONNECT_PLUGIN);
  options.addr                 = reinterpret_cast<sockaddr *>(&data->m_client_ip);
  options.tag                  = PLUGIN_NAME;
  options.id                   = 0;
  options.buffer_index         = data->m_buffer_index;
  options.buffer_water_mark    = data->m_buffer_water_mark;

  TSVConn const upvc = TSHttpConnectPlugin(&options);

  int const hlen = TSHttpHdrLengthGet(header.m_buffer, header.m_lochdr);

  // set up connection with the HttpConnect server
  m_stream.setupConnection(upvc);
  m_stream.setupVioWrite(m_cont, hlen);
  TSHttpHdrPrint(header.m_buffer, header.m_lochdr, m_stream.m_write.m_iobuf);

  if (TSIsDebugTagSet(PLUGIN_NAME)) {
    std::string const headerstr(header.toString());
    DEBUG_LOG("Headers\n%s", headerstr.c_str());
  }
  // ensure blocks are pulled through to cache
  m_stream.setupVioRead(m_cont, INT64_MAX);

  return true;
}

/**
 * @brief Continuation to close background fetch after
 * writing to cache is complete or error
 *
 */
int
BgBlockFetch::handler(TSCont contp, TSEvent event, void * /* edata ATS_UNUSED */)
{
  BgBlockFetch *bg = static_cast<BgBlockFetch *>(TSContDataGet(contp));

  switch (event) {
  case TS_EVENT_VCONN_WRITE_COMPLETE:
    TSVConnShutdown(bg->m_stream.m_vc, 0, 1);
    break;
  case TS_EVENT_VCONN_READ_READY: {
    TSIOBufferReader const reader = bg->m_stream.m_read.m_reader;
    if (nullptr != reader) {
      int64_t const avail = TSIOBufferReaderAvail(reader);
      TSIOBufferReaderConsume(reader, avail);
      TSVIO const rvio = bg->m_stream.m_read.m_vio;
      TSVIONDoneSet(rvio, TSVIONDoneGet(rvio) + avail);
      TSVIOReenable(rvio);
    }
  } break;
  case TS_EVENT_NET_ACCEPT_FAILED:
  case TS_EVENT_VCONN_INACTIVITY_TIMEOUT:
  case TS_EVENT_VCONN_ACTIVE_TIMEOUT:
  case TS_EVENT_ERROR:
    bg->m_stream.abort();
    TSContDataSet(contp, nullptr);
    delete bg;
    TSContDestroy(contp);
    break;
  case TS_EVENT_VCONN_READ_COMPLETE:
  case TS_EVENT_VCONN_EOS:
    bg->m_stream.close();
    TSContDataSet(contp, nullptr);
    delete bg;
    TSContDestroy(contp);
    break;
  default:
    DEBUG_LOG("Unhandled bg fetch event:%s (%d)", TSHttpEventNameLookup(event), event);
    break;
  }
  return 0;
}